944,131 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2307
  • C++ RSS
Nov 3rd, 2007
0

declaring objects within objects from main

Expand Post »
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();
Similar Threads
Reputation Points: 15
Solved Threads: 5
Junior Poster in Training
phalaris_trip is offline Offline
91 posts
since Apr 2007
Nov 3rd, 2007
0

Re: declaring objects within objects from main

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 ?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 3rd, 2007
0

Re: declaring objects within objects from main

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.
Reputation Points: 15
Solved Threads: 5
Junior Poster in Training
phalaris_trip is offline Offline
91 posts
since Apr 2007
Nov 3rd, 2007
0

Re: declaring objects within objects from main

you can make CAnimate a pointer
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 3rd, 2007
0

Re: declaring objects within objects from main

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.
Reputation Points: 15
Solved Threads: 5
Junior Poster in Training
phalaris_trip is offline Offline
91 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Tic-Tac-Toe
Next Thread in C++ Forum Timeline: Need help in my C++ Assignment





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


Follow us on Twitter


© 2011 DaniWeb® LLC