SomeClass & a = * new SomeClass (...)
I came across a construct that I haven't seen before and wonder what its purpose is:
{
SomeClass &a = *new SomeClass(...);
a.someFunc();
}
If my understanding is correct, a
is a link to SomeClass
and created via new
. What purpose does this serve? Ya, why wasn't it written like SomeClass a(...)
? Also, is this code a memory leak? I assume this is the case since it new
is called without a match delete
.
What is it for?
Generation generation on stack overflow. Or perhaps someone is deliberately trying to fire him.
why couldn't it be written as SomeClass a (...)
It had to be.
Also, is this code a memory leak? I assume this is happening as it
new
is called without a matchdelete
.
Yes indeed.
source to share
This means that it SomeClass
is a weird custom object that can only be created on the heap, not the stack, or statically. Therefore, you should look for other strange things that SomeClass
does:
-
it can create a new thread in its constructor that takes ownership of the object.
-
it is likely that some method of the object will implicitly delete it at some point.
However, this probably indicates that it is SomeClass
badly mangled, perhaps to try and encapsulate some behavior written in some other programming language.
source to share