modemer wrote:
I saw someone use the following code:
void func(MyClass *& myCls)
{
myCls->test();
}
// call func():
func(new MyClass);
Some compiler could compile, but some not.
I don't understand the meaning of function prototype. I guess it's not
a stand C++ prototype. Please don't guess like me if you are not 100%
understanding it :-) because probably I'll have further question if
you answer.
There is no prototype here. The function 'func' as defined/declared,
takes one argument which is a reference to a non-const pointer to
an object of MyClass.
A Standard-compliant compiler should not compile
func(new MyClass);
because the 'new' expression returns an r-value and a reference to
a non-const pointer cannot be bound to an r-value. You can change
it to
void func(MyClass * const & myCls)
{
myCls->test();
}
then all compilers should compile
func(new MyClass);
, however, this will be a memory leak because the value of the pointer
obtained from 'new' is lost and the memory is never freed and the object
is never destroyed.
V