Redian新闻
>
C++ class cross reference problem
avatar
C++ class cross reference problem# Programming - 葵花宝典
h*t
1
Could any C++ guru help me? Thanks a million in advance.
I need to write two classes each of which refers the other class. However, I
could not compile it. Below is a sample code to show what I really need. It
has two files, test.h and test.cc. The implementation is not given; however,
it could be compiled with "g++ -c test.cc",
/******* beginning of the source code of test.h *********/
#include
class index_object_1;
class index_object_2;
class index_object_1_comparator;
class index_object
avatar
y*i
2
///
// Change reference to pointer
#include
class index_object_1;
class index_object_2;
class index_object_2_comparator
{
public:
bool operator() (const index_object_2 * lhs, const index_object_2 * rhs)
const
{
return true;
}
};
class index_object_1_comparator {
public:
bool operator() (const index_object_1 * lhs, const index_object_1 * rhs)
const
{
return true;
}
};
class index_object_1 {
private:
double index;
std::set
【在 h******t 的大作中提到】
: Could any C++ guru help me? Thanks a million in advance.
: I need to write two classes each of which refers the other class. However, I
: could not compile it. Below is a sample code to show what I really need. It
: has two files, test.h and test.cc. The implementation is not given; however,
: it could be compiled with "g++ -c test.cc",
: /******* beginning of the source code of test.h *********/
: #include
: class index_object_1;
: class index_object_2;
: class index_object_1_comparator;

avatar
h*t
3
Thanks a lot. It certainly works. I would like to learn more from you. Why can
I not use reference? By using pointers, I have to maintain memory allocation
on my own. For example, if I have a set like
std::set s1;
Whenever I insert a pointer to an object of index_object_1 to the set, I have
to deallocate it when I finish with the object. Sometimes, it is hard to track
if I release all the memory allocated. Is the way you suggested the only way
to make

【在 y****i 的大作中提到】
: ///
: // Change reference to pointer
: #include
: class index_object_1;
: class index_object_2;
: class index_object_2_comparator
: {
: public:
: bool operator() (const index_object_2 * lhs, const index_object_2 * rhs)
: const

avatar
y*i
4
According to my understanding:
when you declare a variable(class type), the compiler need its information
to allocate memory
class A
{
...
};
A a;
However, for a pointer, it will allocate only 4 byte(on 32-bit machine) for it
.
A *p;
This is the only way I know at present. So you have to manage memory by
youself.
track
way
Not hard. you retrieve the pointer to from the set, delete it whenever you
finished the use.
avatar
c*e
5
all what u said is correct, another thing to be noted is that you can NOT
have a reference referencing nothing.

it

【在 y****i 的大作中提到】
: According to my understanding:
: when you declare a variable(class type), the compiler need its information
: to allocate memory
: class A
: {
: ...
: };
: A a;
: However, for a pointer, it will allocate only 4 byte(on 32-bit machine) for it
: .

avatar
h*t
6
Thanks a lot! Very good advice. Now I am thinking if I need to change my other
sets in my applicaiton to set to set to
make consistent. It is easy to get confused later on when I look at two
iterators, one is pointing to "some_object" and another one is pointing to "
some_object*".

it

【在 y****i 的大作中提到】
: According to my understanding:
: when you declare a variable(class type), the compiler need its information
: to allocate memory
: class A
: {
: ...
: };
: A a;
: However, for a pointer, it will allocate only 4 byte(on 32-bit machine) for it
: .

avatar
h*t
7
what do you mean? could you please elaborate a little bit more? Thanks!

【在 c********e 的大作中提到】
: all what u said is correct, another thing to be noted is that you can NOT
: have a reference referencing nothing.
:
: it

avatar
y*i
8
When you declare a reference
int a= 3;
int &n = a; // must init it

【在 h******t 的大作中提到】
: what do you mean? could you please elaborate a little bit more? Thanks!
avatar
N*m
9
I only change test.h to the following. It passed the compilation. But I do
not
know why?
test.h
=============================
#include
class index_object_1_comparator;
class index_object_2_comparator;
class index_object_2;
class index_object_2_comparator {
public:
bool operator() (const index_object_2 & lhs, const index_object_2 & rhs)
const;
};
class index_object_1 {
private:
double index;
std::set::iterator iter;
public:
index_ob
avatar
d*p
10
Your code is perfect if you only swap the class declaration/definitons as
below:
index_obj_1 class forward declaration
index_obj_2 class forward declaration
index_obj_1_comparator definition
index_obj_2_comparator definition
index_obj_1 class definition
index_obj_2 class definition
No need to change reference to pointer because in nature they are identical.
avatar
N*m
12
this is very helpful to green hands like me.

【在 t****t 的大作中提到】
: You only need to know one thing:
: STL container does not require the element type be complete at the time of
: declaration, until you instantiate a member that requires an element to be
: complete. However, it does require the comparator to be complete.
: For example:
: class A;
: class A_comp;
: class B {
: std::vector va1; // OK
: std::set
sa1; // OK

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。