Jump to content
  • Advertisement
Sign in to follow this  
thedevdan

Weird trick.

This topic is 5057 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

class SomeClass
{
public:
   SomeClass(int lots, int of, int needed, int parameters);
...
};

SomeClass MakeSomeClass(int lots, int of, int needed, int parameters)
{
   SomeClass someClass(lots, of, needed, parameters);
   return someClass;
}




If you don't see what I'm doing, I'm just trying to create a short-lived object without having to use new. Is this an abomination (or is it just too slow)? EDIT: Clarity. [Edited by - thedevdan on September 13, 2004 8:06:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
This doesn't replicate new (doesn't allocate), and a normal constructor call can be used instead of this in normal situations.

i.e
SomeClass s(...);
//
s = SomeClass(...);

Share this post


Link to post
Share on other sites
Actually, this is useful where SomeClass is a templated type and the template arguments can be inferred from the parameters of the make function, as per std::pair and make_pair.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The return statement is bogus. Once executed, the stack frame
is out of scope and the returned object may be garbage bits.

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Hare
...a normal constructor call can be used instead of this in normal situations....


Why didn't I think of that? [embarrass]

Well, at least they're equivelant.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
The return statement is bogus. Once executed, the stack frame
is out of scope and the returned object may be garbage bits.

Regards,


Actualy I was kinda thinking something like that could happen.

/MindWipe

Share this post


Link to post
Share on other sites
Quote:
Original post by MindWipe
Quote:
Original post by Anonymous Poster
The return statement is bogus. Once executed, the stack frame
is out of scope and the returned object may be garbage bits.

Regards,


Actualy I was kinda thinking something like that could happen.

/MindWipe


No, it returns by value, not by reference.

Share this post


Link to post
Share on other sites
This is a godsend with templated types. Instead of
std::pair<std::vector<std::vector<int> >, std::vector<std::vector<int> > >(a, b);
you wrtie
std::make_pair(a, b);

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Hare
This is a godsend with templated types. Instead of
std::pair<std::vector<std::vector<int> >, std::vector<std::vector<int> > >(a, b);
you wrtie
std::make_pair(a, b);


Thanks. [smile]

But my way requires an extra temporary to be created.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!