943,991 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1271
  • C++ RSS
Dec 2nd, 2007
0

Creating objects within objects dynamically

Expand Post »
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.
Similar Threads
Reputation Points: 15
Solved Threads: 5
Junior Poster in Training
phalaris_trip is offline Offline
91 posts
since Apr 2007
Dec 2nd, 2007
0

Re: Creating objects within objects dynamically

>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:
C++ Syntax (Toggle Plain Text)
  1. anims["AnimNormal"];
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 2nd, 2007
0

Re: Creating objects within objects dynamically

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

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: Searching a List of Structured Data
Next Thread in C++ Forum Timeline: c++ letter coding





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


Follow us on Twitter


© 2011 DaniWeb® LLC