Creating an object within a member function

Hello everyone!

I have an assignment that states that I have to create a new object using operator overloading, I feel like I might have misinterpret the instructions.

The exact instructions are:

O3 = O1 + O2
creates a new object called O3, which consists of O1+O2
(duplicated elements are allowed)


I have no idea if that's even possible... is it possible to create an object variable in the global namespace INSIDE a member function?

I tried
1
2
3
4
5
ObjectClass operator=(Const ObjectClass& right) const{
    ObjectClass Object;
    // ... some manipulations
    return Object;
}

But then how would I create the object referring specifically to what the object was called
(i.e. ObjectClass O3, if O3 is on the left side?)
Maybe ObjectClass this;?


Or am I just overthinking it and O3 has to be declared first?


Any opinions/ideas?
Last edited on
The actual line would be ObjectClass O3 = O1 + O2; - your code is not responsible for creating (or even knowing about) O3. Just return an instance by value and O3 will be constructed from it.
Last edited on
O3 = O1 + O2
This code has two operators: + and =. You seem to be trying to combine them into one.

First you'll want the + operator, which will take 2 const objects as input, and return a new object as its output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Foo
{
    int v;

public:
    Foo operator + (const Foo& right) const
    {
        Foo sum; // <- our output
        sum.v = this->v + right.v;  // <- sum of the two objects being added:  'this' and 'right'
        sum.v = v + right.v;  // <- alternative way to do the same thing ('this' is optional)

        return sum;  // <- return the sum
    }
};


So as you can see, the + operator takes 2 const objects as input (the left and right side of the operator), and returns a new object which contains their sum.

In the O3 = O1 + O2; example, O1 would become 'this', and O2 would become 'right'. Both should be constant because you would not expect that expression to modify O1 or O2.

After the + operator resolves, you are conceptually left with this:
1
2
O3 = O1 + O2; // <- start with this
O3 = sum; // <- becomes this... 'O1 + O2' operator overload called, returned 'sum' object 


This is where the = operator takes over. Now we have a left side (O3) which is NON-const (because we are modifying it), and a right side (sum) which [b]IS[b] const (because it is not being modified)

1
2
3
4
5
6
7
Foo& operator = (const Foo& right)  // <- function is not const because we don't want 'this' to be const
{
    // same idea here, 'this' is the left side of the '=' and 'right' is the right side.
    v = right.v; // <- do the assignment to modify 'this'

    return *this;
}



A few things to note about the = operator:

1) The weird return type of 'Foo&'.. and 'return *this;' is a reference trick to allow assignment chainging:
 
O3 = O2 = O1;  // <- legal with above code 


2) You generally do not need to overload the = operator, as the compiler will provide one for you. You only need to overload your own if your class has pointers for members or does any kind of memory management (which you generally should avoid by using smart pointers or container classes).
Thank you very much LB and especially Disch!

My object is actually a linked list, but as LB stated my teacher actually ended up replying that there are two things overloading with operator= I have to work with.

The teacher states there are two cases:

1) Assignment (as Disch stated)

1
2
ObjectClass O3;
O3 = O1 + O2  


For the assignment part I actually understood it already, but the const /*and*/ *this tip from Disch clarified a lot of what I didn't understand.

2) Copy constructor (as LB stated)

1
2
ObjectClass O3 = O1 + O2;
//To me I think it's just declare and assigning in one line. 


I'm actually not sure about the claim of using a copy constructor... Is that even possible?
In my opinion both:

1
2
3
4
5
ObjectClass O3 = O1 + O2;
//and
ObjectClass O3;
O3 = O1 + O2;
//They both invoke the assignment operator... (yes?) 


Don't both instances invoke the assignment operator only?
And if not how would one overload the copy constructor using the overloaded assignment operator?
An equals sign on the same line as the declaration does not use the copy assignment operator, instead it directly uses the copy constructor. These two lines are identical:
1
2
ObjectClass O3 = O1 + O2;
ObjectClass O3 (O1 + O2);
Hmmmm
So if I read correctly, if I had already defined and implemented a copy constructor such as

1
2
3
4
5
6
7
ObjectClass(const ObjectClass &);

ObjectClass::ObjectClass(const ObjectClass &placeholder) :
    // ... some code
{
    // ... some code
}

Then I wouldn't need to make another one since
1
2
ObjectClass O3 = O1 + O2;
//will call it anyways? 


Will call it anyways?
Last edited on
Yes. I'm not sure how you even would create "another one" ;)
Last edited on
Topic archived. No new replies allowed.