Class Return Itself

Hi there,

Is it possible to set a class to return itself?

Let’s say we have CustomClass and its void doSomething(param1, param2) within.

Basically I need to make some calculation of parameters and return a new CustomClass object or Custom class which is calling function.

CustomClass CustomClass::DoSomething(param1, param2){

if(...) return itself else return new CustomClass


The problem that I couldn’t use ‘this’, I have tried to do it with pointers, without any success.

(CustomClass) custom2 = custom1.DoSomething(&custom1, param1, param2);

DoSomething(CustomClass custom_, param1, param2){
return *custom_; ????????

Can anyone help me?

this is a pointer so you need to dereference it to return it by value:

CustomClass CustomClass::DoSomething(param1, param2){
        return *this;
    } else {
        return CustomClass()
1 Like