由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++的一个问题
相关主题
C++ question在 windows下的C++开发平台是不是 Dev-C++?
Interview question - use a STL vector object as an argument of function.帮忙看一个code
问个g++的问题c++越写越没有信心,觉得自己水平弱的不行
请教 C++的一个困惑 (operator delete)菜鸟读C++ STL源程序的疑问
[合集] 基类函数已经是virtual了,为啥子类还要virtual啊?一道C++ STL面试题 (转载)
C++ Template Question关于C++ STL编译的疑问
c++ interview: iterator 和 pointer区别?一个C++ operator new的重载问题
问一个OOP的C++问题一个C++ template的问题
相关话题的讨论汇总
话题: inside话题: foo话题: void话题: int
进入Programming版参与讨论
1 (共1页)
d****i
发帖数: 4809
1
怎么在一个函数中调用某个类的数组?比如下面:
class A
{
public:
DoSomething() {...}
};
int main()
{
A a[10];
foo(a);
}
void foo(A *a)
{
int i;
for(i=0;i<10;i++)
a[i].DoSomething();
}
编译会通不过,怎么改才能实现在foo()中的循环呢?
S**I
发帖数: 15689
2
这跟数组有什么关系?foo在main之前没有declare,当然编译通不过了。

【在 d****i 的大作中提到】
: 怎么在一个函数中调用某个类的数组?比如下面:
: class A
: {
: public:
: DoSomething() {...}
: };
: int main()
: {
: A a[10];
: foo(a);

d****i
发帖数: 4809
3
写的时候忘了,foo()已经declare过了,还是不行,是不是foo()中的a[i]不能象int,
char等基本类型那样用pointer arithmetic来derefence?

【在 S**I 的大作中提到】
: 这跟数组有什么关系?foo在main之前没有declare,当然编译通不过了。
S**I
发帖数: 15689
4
你用的什么编译器?GCC没问题。

【在 d****i 的大作中提到】
: 写的时候忘了,foo()已经declare过了,还是不行,是不是foo()中的a[i]不能象int,
: char等基本类型那样用pointer arithmetic来derefence?

z****e
发帖数: 2024
5
run the following!
i didn't see any problem.
#include
using std::cout;
using std::endl;
class A{
static int cnt;
int id;
public:
A():id(++cnt){}
void DoSomething() {cout< };
int A::cnt=0;
void foo(A *a)
{
int i;
for(i=0;i<10;i++)
a[i].DoSomething();
}
int main()
{
A a[10];
foo(a);
}
z****e
发帖数: 2024
6
you may need to add a return value for your member function dosomething, in
case you missed.
e**u
发帖数: 409
7
报啥错?

【在 d****i 的大作中提到】
: 写的时候忘了,foo()已经declare过了,还是不行,是不是foo()中的a[i]不能象int,
: char等基本类型那样用pointer arithmetic来derefence?

R****a
发帖数: 199
8
I think the major part is no problem. The following is my code and it works
by g++.
<<>
#include
class A
{
public:
void DoSomething() {printf("Inside! \n");}
};
void foo(A *a)
{
int i;
for(i=0;i<10;i++)
a[i].DoSomething();
}
int main()
{
A a[10];
foo(a);
}
build by command:
g++ try1.cc -o try1.out
run try1.out
<>
# ./try1.out
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Do you have the compile error? You m

【在 d****i 的大作中提到】
: 怎么在一个函数中调用某个类的数组?比如下面:
: class A
: {
: public:
: DoSomething() {...}
: };
: int main()
: {
: A a[10];
: foo(a);

d****i
发帖数: 4809
9
Thanks, I tried again and it works. No error at all.

works

【在 R****a 的大作中提到】
: I think the major part is no problem. The following is my code and it works
: by g++.
: <<>
: #include
: class A
: {
: public:
: void DoSomething() {printf("Inside! \n");}
: };
: void foo(A *a)

r********t
发帖数: 395
10

用向量的引用作为函数参数,不要用数组

【在 d****i 的大作中提到】
: 怎么在一个函数中调用某个类的数组?比如下面:
: class A
: {
: public:
: DoSomething() {...}
: };
: int main()
: {
: A a[10];
: foo(a);

d****i
发帖数: 4809
11
Do you mean use void foo(vector& a) instead of void foo(A *a)? What is
the advantage?

【在 r********t 的大作中提到】

:
: 用向量的引用作为函数参数,不要用数组

z****e
发帖数: 2024
12
probably void foo(vector &)
or
void foo(vector > smp);
arrays are very bad with polymorphism.
array arithmetic usually doesn't work when virtual call is involved.

【在 d****i 的大作中提到】
: Do you mean use void foo(vector& a) instead of void foo(A *a)? What is
: the advantage?

d****i
发帖数: 4809
13
So why use pointer of A instead of A in the template? If use void foo(vector
& a), one can simply use
void foo(vector
& a)
{
int i;
for(i=0;i a[i].DoSomething();
}
So why use vector & in the function argument?

【在 z****e 的大作中提到】

: probably void foo(vector &)
: or
: void foo(vector > smp);
: arrays are very bad with polymorphism.
: array arithmetic usually doesn't work when virtual call is involved.

d****i
发帖数: 4809
14
An other question is: What is dynamic memory allocation is not allowed? Then
using STL such as vector will not be available. So is there a solution in
this case if class A involves with polymorphism but no STL is allowed to be
used?

【在 z****e 的大作中提到】
: probably void foo(vector &)
: or
: void foo(vector > smp);
: arrays are very bad with polymorphism.
: array arithmetic usually doesn't work when virtual call is involved.

1 (共1页)
进入Programming版参与讨论
相关主题
一个C++ template的问题[合集] 基类函数已经是virtual了,为啥子类还要virtual啊?
vector析构的时候怎么办?C++ Template Question
C++ vector 到底能多大c++ interview: iterator 和 pointer区别?
C++ vector 一边遍历一边删问一个OOP的C++问题
C++ question在 windows下的C++开发平台是不是 Dev-C++?
Interview question - use a STL vector object as an argument of function.帮忙看一个code
问个g++的问题c++越写越没有信心,觉得自己水平弱的不行
请教 C++的一个困惑 (operator delete)菜鸟读C++ STL源程序的疑问
相关话题的讨论汇总
话题: inside话题: foo话题: void话题: int