Problem with objects having objects as attributes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 9
Reputation: gallas is an unknown quantity at this point 
Solved Threads: 0
gallas gallas is offline Offline
Newbie Poster

Problem with objects having objects as attributes

 
0
  #1
Nov 2nd, 2004
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
  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
  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
  1. #include "classone.hh"
  2.  
  3. Classone::Classone(){}

classtwo.cc
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Problem with objects having objects as attributes

 
0
  #2
Nov 2nd, 2004
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):
  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 9
Reputation: gallas is an unknown quantity at this point 
Solved Threads: 0
gallas gallas is offline Offline
Newbie Poster

Re: Problem with objects having objects as attributes

 
0
  #3
Nov 2nd, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Problem with objects having objects as attributes

 
0
  #4
Nov 4th, 2004
"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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 9
Reputation: gallas is an unknown quantity at this point 
Solved Threads: 0
gallas gallas is offline Offline
Newbie Poster

Re: Problem with objects having objects as attributes

 
0
  #5
Nov 8th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with objects having objects as attributes

 
0
  #6
Nov 8th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 9
Reputation: gallas is an unknown quantity at this point 
Solved Threads: 0
gallas gallas is offline Offline
Newbie Poster

Re: Problem with objects having objects as attributes

 
0
  #7
Nov 8th, 2004
>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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 1
Reputation: som is an unknown quantity at this point 
Solved Threads: 0
som som is offline Offline
Newbie Poster

Re: Problem with objects having objects as attributes

 
0
  #8
Dec 3rd, 2004
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
  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
  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
  1. #include "classone.hh"
  2.  
  3. Classone::Classone(){}

classtwo.cc
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: nvanevski is an unknown quantity at this point 
Solved Threads: 1
nvanevski nvanevski is offline Offline
Newbie Poster

Re: Problem with objects having objects as attributes

 
0
  #9
Dec 3rd, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 13
Reputation: britt_boy is an unknown quantity at this point 
Solved Threads: 1
britt_boy britt_boy is offline Offline
Newbie Poster

Re: Problem with objects having objects as attributes

 
0
  #10
Dec 7th, 2004
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.

  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. };
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC