| | |
declaring objects within objects from main
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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();
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();
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.
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
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.
you can make CAnimate a pointer
But you should probably put the code in CPlanet to make it allocate all necessary memory for itself
C++ Syntax (Toggle Plain Text)
class CPlanet { ... CAnimate* m_animateptr; }; int main() { CPlanet planet; planet->m_animateptr = new CAnimate; }
But you should probably put the code in CPlanet to make it allocate all necessary memory for itself
C++ Syntax (Toggle Plain Text)
class CPlanet { public: CPlanet(bool bNeedAnimate = true) { if( bNeedAnimate) m_animateptr = new CAnimate; else m_animateptr = NULL; } ... CAnimate* m_animateptr; };
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.
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

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.
![]() |
Similar Threads
- help:stl vector of user defined objects (C)
- I want to learn - How to create and use objects. (C#)
- link list holding objects (C)
- input from file into class (C++)
- About:Blank Homepage (Viruses, Spyware and other Nasties)
- Problem with objects having objects as attributes (C++)
- Can't remove "about:blank" homepage. Please help. (Viruses, Spyware and other Nasties)
- accessing private data members (C++)
- Possible CWS problem (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Tic-Tac-Toe
- Next Thread: Need help in my C++ Assignment
Views: 1711 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 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 encryption error file format forms fstream function functions game givemetehcodez graph 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 sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






