Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


New objects from old (CLASSES)

This topic is 6056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Does anyone know of a way that I can call a class to make an object, but by only passing a varible as the name.. Here''s the code, that doesnt work.. void sprite::clonesprite(char *directory) { tempsizex=spritex; tempsizey=spritey; callit=textureid; sprite directory("clone"); } As you can see, I''m in a class called clonesprite, but need to create a new object based on the name held in ''directory''. (which is passed to the class in the first place. Help !!!.. please.

Share this post

Link to post
Share on other sites

actually sprite::clonesprite means you're in class sprite defining method clonesprite.

what you need is a copy constructor... which should look like in your class declaration

class sprite {
int spritex, spritey, textureid;
// ...
sprite(const sprite& rhs);
// ...

and you should define it like...

sprite::sprite(const sprite& rhs): spritex(rhs.spritex), spritey(rhs.spritey), textureid(rhs.textureid) {}

I know this is not EXACTLY what you meant to do, but I hope it helps... if the variable names don't match... that's cause you didn't post your class declaration

the way to call that constructor would be like

sprite NewSprite = OldSprite;

"Do it with style or don't bother doing it."

Edited by - xEricx on February 22, 2002 11:21:11 AM

Share this post

Link to post
Share on other sites
It sounds like you need to use the Factory Method design pattern. Look it up on the web and, if you have further questions, post back here.

The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!