| | |
Problem with objects having objects as attributes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 9
Reputation:
Solved Threads: 0
I have a silly problem. I want to make a class (let's call it Classone) that has an object that's an instance of Classtwo as an attribute. So far so good, but when Classtwo has an attribute that's an instance of Classone, I get a compilationerror. I would appreciate if someone could help me out with this problem. Example code follows:
classone.hh
classtwo.hh
classone.cc
classtwo.cc
ok, so this code doesn't do much good, but I hope y'all get the idea.
This problem is causing much frustration so I'm hoping on help quickly.
classone.hh
C++ Syntax (Toggle Plain Text)
#ifndef CLASSONE_H #define CLASSONE_H #include "classtwo.hh" class Classone{ Classtwo ct; public: Classone(); }; #endif
classtwo.hh
C++ Syntax (Toggle Plain Text)
#ifndef CLASSTWO_H #define CLASSTWO_H #include "classone.hh" class Classtwo{ Classone co; public: Classtwo(); }; #endif
classone.cc
C++ Syntax (Toggle Plain Text)
#include "classone.hh" Classone::Classone(){}
classtwo.cc
C++ Syntax (Toggle Plain Text)
#include "classtwo.hh" Classtwo::Classtwo() {}
ok, so this code doesn't do much good, but I hope y'all get the idea.
This problem is causing much frustration so I'm hoping on help quickly.
Yeah, that's a toughie. You have two basic options, someone else may know of another.
1) use a forward declaration for one of them, and then use that class as a POINTER (because the compiler knows that such a class exists, but not how big it is or anything like that):
2) Are they similar enough to have a common superclass? Then you can declare a pointer to the superclass. This only works if one class can call member functions that belong to the superclass:
1) use a forward declaration for one of them, and then use that class as a POINTER (because the compiler knows that such a class exists, but not how big it is or anything like that):
C++ Syntax (Toggle Plain Text)
class Classone; // forward class Classtwo { <blah blah blah> Classone* m_pClassOne; };
2) Are they similar enough to have a common superclass? Then you can declare a pointer to the superclass. This only works if one class can call member functions that belong to the superclass:
C++ Syntax (Toggle Plain Text)
class super { public: CallMe(); }; class Classone : public super { <blah blah blah> super* m_pClasstwo; } class Classtwo : public super { <blah blah blah> super* m_pClassone; }
Last edited by alc6379; Nov 4th, 2004 at 11:49 pm.
•
•
Join Date: Nov 2004
Posts: 9
Reputation:
Solved Threads: 0
Thank you for helping. The pointer-fix works ok, even though it's not exactly what I was hoping for.
Now a related question.
1. I've heard that using pointers makes it much more difficult for the compiler to optimize code. Will the forward declaration and pointer solution be comparatively slow, and is there any way around it?
Now a related question.
1. I've heard that using pointers makes it much more difficult for the compiler to optimize code. Will the forward declaration and pointer solution be comparatively slow, and is there any way around it?
"Comparatively slow"?
You have a CPU capable of adding two registers together in 1 trillionth of a second, and you want to know if a pointer dereference will slow you down?
Dude!
Depending on the processor (Arm? x86?) and the compiler and it's optimizer, referencing something through a pointer will generally take an extra instruction or two over NOT referencing through a pointer. And, there may be some cases where the compiler would have known something more about a specific routine (like its local or inlinable) if you didn't refer to it by pointer. However, unless you are doing this in a very large loop, why spend time saving a millisecond?
My advice: make the code clean looking and readable and make it WORK. Then, if it is slow, figure out what to do next. Most programs wait an intermitible time for disk drives and human input.
You have a CPU capable of adding two registers together in 1 trillionth of a second, and you want to know if a pointer dereference will slow you down?
Dude!
Depending on the processor (Arm? x86?) and the compiler and it's optimizer, referencing something through a pointer will generally take an extra instruction or two over NOT referencing through a pointer. And, there may be some cases where the compiler would have known something more about a specific routine (like its local or inlinable) if you didn't refer to it by pointer. However, unless you are doing this in a very large loop, why spend time saving a millisecond?
My advice: make the code clean looking and readable and make it WORK. Then, if it is slow, figure out what to do next. Most programs wait an intermitible time for disk drives and human input.
•
•
Join Date: Nov 2004
Posts: 9
Reputation:
Solved Threads: 0
The classes will be used in the inner workings of a search through a state tree for a game AI, so in this case I'd say that the pointer referencing is unwanted. I realize that the overhead caused by this usually doesn't affect performance notably.
(And isn't it closer to one addition in one billionth of a second ;-)
Anyway, thank you for your help this far.
(And isn't it closer to one addition in one billionth of a second ;-)
Anyway, thank you for your help this far.
>1. I've heard that using pointers makes it much more difficult for the compiler to optimize code.
You heard wrong. If a compiler fails to optimize pointers properly, it's extremely unlikely that all other compilers do as well.
>so in this case I'd say that the pointer referencing is unwanted
If performance is this much of an issue, you need to be using inline assembly.
You heard wrong. If a compiler fails to optimize pointers properly, it's extremely unlikely that all other compilers do as well.
>so in this case I'd say that the pointer referencing is unwanted
If performance is this much of an issue, you need to be using inline assembly.
New members chased away this month: 4
•
•
Join Date: Nov 2004
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by gallas
I have a silly problem. I want to make a class (let's call it Classone) that has an object that's an instance of Classtwo as an attribute. So far so good, but when Classtwo has an attribute that's an instance of Classone, I get a compilationerror. I would appreciate if someone could help me out with this problem. Example code follows:
classone.hh
C++ Syntax (Toggle Plain Text)
#ifndef CLASSONE_H #define CLASSONE_H #include "classtwo.hh" class Classone{ Classtwo ct; public: Classone(); }; #endif
classtwo.hh
C++ Syntax (Toggle Plain Text)
#ifndef CLASSTWO_H #define CLASSTWO_H #include "classone.hh" class Classtwo{ Classone co; public: Classtwo(); }; #endif
classone.cc
C++ Syntax (Toggle Plain Text)
#include "classone.hh" Classone::Classone(){}
classtwo.cc
C++ Syntax (Toggle Plain Text)
#include "classtwo.hh" Classtwo::Classtwo() {}
ok, so this code doesn't do much good, but I hope y'all get the idea.
This problem is causing much frustration so I'm hoping on help quickly.
classtwo; //forward declaration
classone
{
public:
classone(classtwo* ptrclasstwo){m_ptrclasstwo = ptrclasstwo;}
classtwo* m_ptrclasstwo;
};
classtwo
{
public
classtwo(){m_ptrclassone = new classone(this);}
classone* m_ptrclassone;
};
this way u can access from classone the classtwo's members using m_ptrclasstwo, and as well as u can access classone's members using the member variable m_ptrclassone. i think this will serve ur purpose.
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by gallas
Thank you for helping. The pointer-fix works ok, even though it's not exactly what I was hoping for.
Now a related question.
1. I've heard that using pointers makes it much more difficult for the compiler to optimize code. Will the forward declaration and pointer solution be comparatively slow, and is there any way around it?
Bottom line : use pointers and do not be afraid. Especially with classes, where additional processing (virtual method tables etc) adds a lot of overhead to the dereferencing itself.
•
•
Join Date: Dec 2004
Posts: 13
Reputation:
Solved Threads: 1
Just to agitate things up a little. The problem can aslo be solved by use of const or non-const reference members and is similar to the pointer version.
C++ Syntax (Toggle Plain Text)
class classone { public: classone(const classtwo& classtworef) :m_classtworef(classtworef) { //reference members can only be initialized in member initialization list } const classtwo& m_classtworef; }; class classtwo { public: classtwo(const classone& classoneref) : m_classoneref(classoneref) { //reference members can only be initialized in member initialization list } const classone& m_classoneref; };
![]() |
Similar Threads
- ASP.NET - session objects Problem (ASP.NET)
- high number of objects for a data structure (C++)
- Problem with arrays or objects and classes (C++)
- Problem deserializing objects from multiple classes (C#)
- Passing arrays of objects to functions (C++)
Other Threads in the C++ Forum
- Previous Thread: School Project
- Next Thread: the beginner needs help
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






