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;
}
Weird trick.
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(...);
i.e
SomeClass s(...);
//
s = SomeClass(...);
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.
The return statement is bogus. Once executed, the stack frame
is out of scope and the returned object may be garbage bits.
Regards,
is out of scope and the returned object may be garbage bits.
Regards,
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.
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
Quote:Original post by MindWipeQuote: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.
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);
std::pair<std::vector<std::vector<int> >, std::vector<std::vector<int> > >(a, b);
you wrtie
std::make_pair(a, b);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement