declaring objects within objects from main

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2007
Posts: 84
Reputation: phalaris_trip is an unknown quantity at this point 
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

declaring objects within objects from main

 
0
  #1
Nov 3rd, 2007
I think I've been up too long at this stage.. I think this is a really stupid question:

How do I declare all my objects from main() ?
I have an object 'sun' of the class 'CPlanet' and within that class there is an instance of the class 'CAnimation'. But I would like to instantiate everything from main..
So I want to be able to create (or not create) a 'CAnimation' object within 'sun' from main();
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: declaring objects within objects from main

 
0
  #2
Nov 3rd, 2007
You mean remove it from CPlanet? or leave it there and instantiate another instance of it in main(). I assume the purpose of CAnimation is to make the class rotate on its axis, so removing it from CPlanet would not be a very good idea. If you instantiated an instance of CAnimation directly in main() what would it do? What is it supposed to animate ?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 84
Reputation: phalaris_trip is an unknown quantity at this point 
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Re: declaring objects within objects from main

 
0
  #3
Nov 3rd, 2007
Hi Ancient Dragon,

Sorry if I didn't explain it clearly..
CPlanet is a generic class that instantiates a mass in space. The mass may or may not be animated. In order for it to be animated there must be an object created within CPlanet that makes animation possible. I do not want this object created each time I make a planet, because some of the planets are just red circles...

So if I have created a CPlanet object called planet, can I then create a CAnimation object called animation within that? And can I do this from main?

Something like:

int main ()
{

...
CPlanet planet;
planet.CAnimation animation;
...
return 0;

}

of-course that code doesn't work, but you get what I mean, yeah?

Thanks for your reply
Last edited by phalaris_trip; Nov 3rd, 2007 at 7:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: declaring objects within objects from main

 
0
  #4
Nov 3rd, 2007
you can make CAnimate a pointer
  1. class CPlanet
  2. {
  3. ...
  4. CAnimate* m_animateptr;
  5. };
  6.  
  7. int main()
  8. {
  9. CPlanet planet;
  10. planet->m_animateptr = new CAnimate;
  11. }

But you should probably put the code in CPlanet to make it allocate all necessary memory for itself
  1. class CPlanet
  2. {
  3. public:
  4. CPlanet(bool bNeedAnimate = true)
  5. {
  6. if( bNeedAnimate)
  7. m_animateptr = new CAnimate;
  8. else
  9. m_animateptr = NULL;
  10. }
  11. ...
  12. CAnimate* m_animateptr;
  13. };
Last edited by Ancient Dragon; Nov 3rd, 2007 at 7:42 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 84
Reputation: phalaris_trip is an unknown quantity at this point 
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Re: declaring objects within objects from main

 
0
  #5
Nov 3rd, 2007
Yes, the pointer solution is what I'm looking for.

The reason I don't want to use the second approach is because I need to pass a lot of additional parameters to the CAnimate object.. so this would mean I need intermediate functions to transpose those parameters from CPlanet to CAnimate.. this would cause my code to bloat a bit I think..

Thanks a lot for the fast reply and sorry for stupid thread... I've been up for a long time now and my mind was going numb
Last edited by phalaris_trip; Nov 3rd, 2007 at 8:48 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1711 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC