user3443063
user3443063

Reputation: 1615

is it possible to use a class as a return type of a the own method

Is it possible to use a class as a return type of a class method (ErgbnisAusFortran) like this:

class ErgbnisAusFortran
{
public:
   ErgbnisAusFortran();
private:
   int Var_a;
   int Var_b ;  
public: 
   int getVar_a() const { return Var_a; }
   int getVar_res() const { return Var_res; }

   void setVar_res(int input)      {Var_res = input;}
   void setVar_a(int input)        {Var_a = input;}

   ErgbnisAusFortran calculateResults(EingabeWerte Ein);    
};

Upvotes: 4

Views: 4566

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726879

Absolutely, this is possible. This is also quite common - for example, Factory Method pattern can be implemented within a single class, in which case member functions would be returning instances of the class of which they are members. Another common situation when this is done is immutable classes producing modified versions of themselves.

Although you can do it in nearly all situations, there may be situations when you should not do it, opting for creating a separate class with a function returning instances of your class. The main criterion is whether it makes sense from a logical point of view.

Upvotes: 3

Related Questions