Creating objects within objects dynamically

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

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

Creating objects within objects dynamically

 
0
  #1
Dec 2nd, 2007
So let's say I have a class called CSprite which loads a sprite sheet. Within that class will be at least one CAnimation object which takes user-defined snippets from the sprite sheet and loops them.. so for example I might want:
CAnimation m_AnimNormal, m_AnimOnFire;
inside the actual CSprite object.

The problem is that I might have 20 or 30 such loops per sprite sheet. So I will need a vector (std::vector<CAnimation> anims) or something, but I don't want the programmer to just refer to them as "anims[i]". I would like meaningful names, so that later I can just say something like:
m_Sprite.DisplaySurface(m_AnimNormal);
instead of
m_Sprite.DisplaySurface(anims[i]);

But how do I go about creating useable names dynamically? The only thing I can think of is anims[i].push_back(whatever), which isn't very descriptive.

Of-course I could delve into strings and associate some string in a separate vector with each animation object and then set up a for loop to check the strings each time... but for some reason this seems like the wrong way to go about it...

Cheers
Last edited by phalaris_trip; Dec 2nd, 2007 at 7:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,656
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: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Creating objects within objects dynamically

 
0
  #2
Dec 2nd, 2007
>But how do I go about creating useable names dynamically?
Use a map instead of a vector, then you can use a string to index the map instead of an integer, which makes things more meaningful:
  1. anims["AnimNormal"];
I'm here to prove you wrong.
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: Creating objects within objects dynamically

 
0
  #3
Dec 2nd, 2007
So you mean something like std::map <const char*, CAnimation> anims; ?

Never used them before but yeah I guess that would do the trick.. thanks for quick reply,
Last edited by phalaris_trip; Dec 2nd, 2007 at 7:29 pm.
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