由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ class cross reference problem
相关主题
c++ private 问题问问开发ios的,有用C++来组织代码的么?
请教一个boost::bind的问题A C++ STL question
function declaration这段 C++ 怎么改才能编译?
问一个C++ set和unordered_set iterator的问题C++ 模板编译错误?
template 疑问C++ 用户定义exception的标准用法是什么?
C++ syntax questionconst_reverse_iterator和reverse_iterator有什么区别? (转载)
C++ Q05: pointer to constant variablereturn Triangular_iterator( _beg_pos );意思
一个诡异的const_cast问题基本功不扎实,问个问题
相关话题的讨论汇总
话题: index话题: object话题: class话题: comparator话题: set
进入Programming版参与讨论
1 (共1页)
h******t
发帖数: 4
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
y****i
发帖数: 156
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;

h******t
发帖数: 4
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

y****i
发帖数: 156
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.
c********e
发帖数: 383
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
: .

h******t
发帖数: 4
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
: .

h******t
发帖数: 4
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

y****i
发帖数: 156
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!
N***m
发帖数: 4460
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
d****p
发帖数: 685
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.
t****t
发帖数: 6806
11
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
std::vector
va2(10); // error: initialize a vector with 10 elements but
the element is incomplete
std::vector
va3(0); // error: although you
N***m
发帖数: 4460
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

1 (共1页)
进入Programming版参与讨论
相关主题
基本功不扎实,问个问题template 疑问
这段C++代码有啥问题C++ syntax question
有没有大牛说说C里边for循环的坏处C++ Q05: pointer to constant variable
[转载] Tcl/C++ question一个诡异的const_cast问题
c++ private 问题问问开发ios的,有用C++来组织代码的么?
请教一个boost::bind的问题A C++ STL question
function declaration这段 C++ 怎么改才能编译?
问一个C++ set和unordered_set iterator的问题C++ 模板编译错误?
相关话题的讨论汇总
话题: index话题: object话题: class话题: comparator话题: set