-1

I am an beginner in C++ and would like to do something like this:

myObj f(){
    // do stuff 
    // return instance of myObj
}

int main(){
    // do stuff
    myObj mO = f(); 
}

What would I have to do, to make this work in C++?

My thoughts were, that I would have to implement an assign operator for the myObj stuct/class or to write another constructor that looks like this myObj::myObj(myObj mO){...}, which I use like this myObj = myObj(f());.

Is this correct?

Do I have to do more, to make this work?

Could you maybe provide a working example?

Thanks!

User12547645
  • 6,955
  • 3
  • 38
  • 69
  • 3
    That's not assignment, it is initialisation. Depending on what your actual class looks like, you probably don't need to write any extra code to perform it. And to learn more, please read a good C++ textbook. –  May 19 '18 at 21:14
  • @NeilButterworth You can accompany such comments with a link to our [list of good C++ books](https://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO May 19 '18 at 21:16

2 Answers2

0

That would almost compile as-is.

//define a class
class myObj {};

// return an instance of the class
myObj f() {
    return myObj{};
}

// call with the same main as in the question:
int main(){
    // do stuff
    myObj mO = f(); 
}
wally
  • 10,717
  • 5
  • 39
  • 72
  • If this compiles that I do not understand why I had problems with this recently. I used something like this, but It did not return the correct myObj. f created the myObj I wanted, but mO was assigned to a default myObj instead – User12547645 May 19 '18 at 21:17
  • @User12547645 If you have problems with some code, better post a question about that specific problem, with a [mcve] to illustrate it. – juanchopanza May 19 '18 at 21:18
  • @juanchopanza I do not have the code anymore, but I will try to reproduce it – User12547645 May 19 '18 at 21:23
0

C++ defines for you a copy constructor, an assignment operator and a move constructor if this can be done trivially; in these cases you should have to do nothing, just return an object instance and the caller will get it.

If the object has however some parts that cannot be copied (for example references) then you need to provide copy constructors and assignment yourself (but may be the class indeed shouldn't be copied or assigned).

There are also other limitations that prevent automatic synthesis of the move constructor (to avoid bugs).

Note also that there are cases in which the C++ compiler will synthesize copy constructor and assignment, but using wrong code. You need to be careful (for example if the class contains naked owning pointers).

For a simple case in which everything works out of the box with no need to do anything consider:

// A bi-dimensional point
struct P2d {
    double x, y;
};

// Computes the middle point given two points
P2d average(P2d a, P2d b) {
    return P2d{(a.x+b.x)/2, (a.y+b.y)/2};
}

As you see nothing is needed in the class to support returning P2d values or accepting P2d parameters.

The compiler in that case automatically completes the definition code to something like:

struct P2d {
    double x, y;

    P2d(const P2d& other)
      : x(other.x), y(other.y)
    {
    }

    P2d& operator=(const P2d& other) {
        x = other.x;
        y = other.y;
        return *this;
    }

    ~P2d() {
    }
};
6502
  • 112,025
  • 15
  • 165
  • 265
  • I don't think whether the special member functions are synthesized or not has much to do with triviality, but rather with whether the resulting code would be safe or consistent (e.g. reference or const members.) – juanchopanza May 19 '18 at 21:41