由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ interdependence question
相关主题
ask a simple question about int pointer.another tougth pointer example
difference between: char** p and char*p[] ??zero-sized array vs pointer
C++里get array size的问题 (转载)C 语言,初学者,简单问题
菜鸟问个C++问题size of structure
问一个defining array 的问题数组问题
问个virtual table 的问题怎么得到char *分配空间的大小?
Array in CA question about class size
问个程序问题C++ template preprocessor
相关话题的讨论汇总
话题: c++话题: class话题: object话题: fix
进入Programming版参与讨论
1 (共1页)
j****i
发帖数: 305
1
A has member object B, and B has A. It won't compile. How to fix this?
My actuall program has much more complicated dependence, and I'm using head
files etc, how to fix that?
Thanks a lot.
class A{
public:
A();
B b;
};
class B{
public:
B();
A a;
};
A::A(){}
B::B(){}
int main(){
A a();
return 0;
}
t****t
发帖数: 6806
2
ask yourself, what is sizeof(A) if this is allowed?
of course it won't compile. C++ isn't java, this is not allowed. for java,
object is just a pointer (or reference); for c++, an object is really an
object.

head

【在 j****i 的大作中提到】
: A has member object B, and B has A. It won't compile. How to fix this?
: My actuall program has much more complicated dependence, and I'm using head
: files etc, how to fix that?
: Thanks a lot.
: class A{
: public:
: A();
: B b;
: };
: class B{

j****i
发帖数: 305
3
Thanks. How do I get around this? Define a parent class of A that does not c
ontain B? I really have no clue.

【在 t****t 的大作中提到】
: ask yourself, what is sizeof(A) if this is allowed?
: of course it won't compile. C++ isn't java, this is not allowed. for java,
: object is just a pointer (or reference); for c++, an object is really an
: object.
:
: head

t****t
发帖数: 6806
4
use pointers to refer each other.

c

【在 j****i 的大作中提到】
: Thanks. How do I get around this? Define a parent class of A that does not c
: ontain B? I really have no clue.

j****i
发帖数: 305
5
You mean void* pointers? If I replace B b; by B* b, it still does not work.

【在 t****t 的大作中提到】
: use pointers to refer each other.
:
: c

t****t
发帖数: 6806
6
write
class B;
before you define A. that's called forward declaration (google it if you don
't know what to do)

【在 j****i 的大作中提到】
: You mean void* pointers? If I replace B b; by B* b, it still does not work.
j****i
发帖数: 305
7
Yeah, that works. Thanks.

don

【在 t****t 的大作中提到】
: write
: class B;
: before you define A. that's called forward declaration (google it if you don
: 't know what to do)

1 (共1页)
进入Programming版参与讨论
相关主题
C++ template preprocessor问一个defining array 的问题
64BIT 软件开发问个virtual table 的问题
新手问一个多维数组传递给函数的问题Array in C
g++ default optimization error问个程序问题
ask a simple question about int pointer.another tougth pointer example
difference between: char** p and char*p[] ??zero-sized array vs pointer
C++里get array size的问题 (转载)C 语言,初学者,简单问题
菜鸟问个C++问题size of structure
相关话题的讨论汇总
话题: c++话题: class话题: object话题: fix