943,493 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4286
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2004
0

Problem with objects having objects as attributes

Expand Post »
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)
  1. #ifndef CLASSONE_H
  2. #define CLASSONE_H
  3.  
  4. #include "classtwo.hh"
  5.  
  6. class Classone{
  7.  
  8. Classtwo ct;
  9.  
  10. public:
  11. Classone();
  12. };
  13. #endif

classtwo.hh
C++ Syntax (Toggle Plain Text)
  1. #ifndef CLASSTWO_H
  2. #define CLASSTWO_H
  3.  
  4. #include "classone.hh"
  5.  
  6. class Classtwo{
  7.  
  8. Classone co;
  9.  
  10. public:
  11. Classtwo();
  12. };
  13. #endif

classone.cc
C++ Syntax (Toggle Plain Text)
  1. #include "classone.hh"
  2.  
  3. Classone::Classone(){}

classtwo.cc
C++ Syntax (Toggle Plain Text)
  1. #include "classtwo.hh"
  2.  
  3. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gallas is offline Offline
9 posts
since Nov 2004
Nov 2nd, 2004
0

Re: Problem with objects having objects as attributes

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):
C++ Syntax (Toggle Plain Text)
  1. class Classone; // forward
  2.  
  3. class Classtwo
  4. {
  5. <blah blah blah>
  6. Classone* m_pClassOne;
  7. };


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)
  1. class super
  2. {
  3. public:
  4. CallMe();
  5. };
  6.  
  7. class Classone : public super
  8. {
  9. <blah blah blah>
  10. super* m_pClasstwo;
  11. }
  12.  
  13. class Classtwo : public super
  14. {
  15. <blah blah blah>
  16. super* m_pClassone;
  17. }
Last edited by alc6379; Nov 4th, 2004 at 11:49 pm.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Nov 2nd, 2004
0

Re: Problem with objects having objects as attributes

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gallas is offline Offline
9 posts
since Nov 2004
Nov 4th, 2004
0

Re: Problem with objects having objects as attributes

"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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Nov 8th, 2004
0

Re: Problem with objects having objects as attributes

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gallas is offline Offline
9 posts
since Nov 2004
Nov 8th, 2004
0

Re: Problem with objects having objects as attributes

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2004
0

Re: Problem with objects having objects as attributes

>If performance is this much of an issue, you need to be using inline assembly.
I am, in some cases, but mostly I'm relying on my old faithful GNU-compiler to do the optimization for me. Thank you for your piece of advice all the same.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gallas is offline Offline
9 posts
since Nov 2004
Dec 3rd, 2004
0

Re: Problem with objects having objects as attributes

Quote 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)
  1. #ifndef CLASSONE_H
  2. #define CLASSONE_H
  3.  
  4. #include "classtwo.hh"
  5.  
  6. class Classone{
  7.  
  8. Classtwo ct;
  9.  
  10. public:
  11. Classone();
  12. };
  13. #endif

classtwo.hh
C++ Syntax (Toggle Plain Text)
  1. #ifndef CLASSTWO_H
  2. #define CLASSTWO_H
  3.  
  4. #include "classone.hh"
  5.  
  6. class Classtwo{
  7.  
  8. Classone co;
  9.  
  10. public:
  11. Classtwo();
  12. };
  13. #endif

classone.cc
C++ Syntax (Toggle Plain Text)
  1. #include "classone.hh"
  2.  
  3. Classone::Classone(){}

classtwo.cc
C++ Syntax (Toggle Plain Text)
  1. #include "classtwo.hh"
  2.  
  3. 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.
u can do this way:
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.
som
Reputation Points: 10
Solved Threads: 0
Newbie Poster
som is offline Offline
1 posts
since Nov 2004
Dec 3rd, 2004
0

Re: Problem with objects having objects as attributes

Quote 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?
I think you should use both solutions : make a forward declaration and use pointer dereferencing. If you don't use pointers, compiler allocates the objects on the stack, which is much slower than working with heap objects due to constant rearanging of the stack frame, besides it takes space in your code ("TEXT") segment. Pointer dereferencing is simply a register-register operation, or if the compiler decides not to optimize it, memory-register operation. Ever since 80486, both memory-register and register-register operations take one CPU cycle to complete (add one cycle penalty for indexed operations) versus 4-cycle PUSH/POP stack operations (a little less on Pentium CPUs). Even an optimizing compiler which would use EBP-based indexing to reach objects on the stack, is a subject to a two-cycle penalty for accessing objects out of +/- 128 bytes from the current ESP.

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nvanevski is offline Offline
12 posts
since Dec 2004
Dec 7th, 2004
0

Re: Problem with objects having objects as attributes

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)
  1. class classone
  2. {
  3. public:
  4. classone(const classtwo& classtworef)
  5. :m_classtworef(classtworef)
  6. {
  7. //reference members can only be initialized in member initialization list
  8. }
  9. const classtwo& m_classtworef;
  10. };
  11.  
  12. class classtwo
  13. {
  14. public:
  15. classtwo(const classone& classoneref)
  16. : m_classoneref(classoneref)
  17. {
  18. //reference members can only be initialized in member initialization list
  19. }
  20. const classone& m_classoneref;
  21. };
Reputation Points: 10
Solved Threads: 1
Newbie Poster
britt_boy is offline Offline
13 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: School Project
Next Thread in C++ Forum Timeline: the beginner needs help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC