Archived

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

pauljg

New objects from old (CLASSES)

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
Hi,

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 {
public:
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