Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects as default values in c++

Tags:

c++

is there a way to have a default Object for parameters in C++ functions? I tried

void func(SomeClass param = new SomeClass(4));

and it worked. However how would I am I supposed to know wheter I have to free the allocated memory in the end? I would like to do the same without pointers, just an Object on the stack. is that possible?

like image 983
Maximosaic Avatar asked Feb 18 '23 20:02

Maximosaic


1 Answers

void func(SomeClass param = new SomeClass(4));

This can't work because new returns a pointer

void func(SomeClass param = SomeClass(4));

should work and the object doesn't need to be freed.

like image 81
stefaanv Avatar answered Feb 28 '23 05:02

stefaanv