why vector assignment operator not invoked on const vector& ?

Z

zhou

Hi there,

I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
vector & type), which makes a copy of the vector returned from myFunc():

vector<myObj*> anotherVec = myFunc();

const vector<myObj *>& myFunc()
{
vector <myObj *> localVec;

// add objects into localVec

return localVec;
}

However, my understanding is that assignment operator takes const Object& type. However, the assignment operator on
vector is not invoked here and therefore vector is not copied. Am I missing anything?

Thanks.

Yan
 
R

Ron Natalie

zhou said:
Hi there,

I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
vector & type), which makes a copy of the vector returned from myFunc():

vector<myObj*> anotherVec = myFunc();

It has nothing to do with constness. The above is NOT an assignment, it is initialization.
The copy-constructor is what is invoked.
 
K

Karl Heinz Buchegger

zhou said:
Hi there,

I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
vector & type), which makes a copy of the vector returned from myFunc():

vector<myObj*> anotherVec = myFunc();

const vector<myObj *>& myFunc()
{
vector <myObj *> localVec;

// add objects into localVec

return localVec;
}

However, my understanding is that assignment operator takes const Object& type. However, the assignment operator on
vector is not invoked here and therefore vector is not copied. Am I missing anything?

Yep.
The above is *not* an assignment. It is an initialization, hence the
copy constructor is called (or not, if it is optimized away by the compiler).
 
A

Andrey Tarasevich

zhou said:
Hi there,

I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
vector & type), which makes a copy of the vector returned from myFunc():

vector<myObj*> anotherVec = myFunc();

This is initialization, not assignment.
const vector<myObj *>& myFunc()
{
vector <myObj *> localVec;

// add objects into localVec

return localVec;
}

However, my understanding is that assignment operator takes const Object& type. However, the assignment operator on
vector is not invoked here and therefore vector is not copied. Am I missing anything?

The copy constructor is supposed to be invoked. However, returning a
reference to a local object is illegal in C++ It causes undefined
behavior. That's what's happening in your code.
 
J

John Harrison

zhou said:
I see. However, why the copy constructor does not copy the vector?

Yan

It does copy the vector, what else would it do?

Why do you think it does not copy the vector?

I think you should post some real.

john
 
K

Kevin Goodsell

zhou said:

I don't, since you didn't bother to quote any context.
However, why the copy constructor does not copy the vector?

A vector's copy constructor does copy. If you have code demonstrating a
case where it doesn't, post it. Otherwise I have no idea what you are
talking about.

-Kevin
 
Z

zhou

Thanks. You are right. The copy consturctor is invoked after the local object is destroyed, thus returning a const &
does no good: it does invoke copy constructor on an already emptied vector.
 
A

Andrey Tarasevich

zhou said:
Thanks. You are right. The copy consturctor is invoked after the local object is destroyed, thus returning a const &
does no good: it does invoke copy constructor on an already emptied vector.

It is not that it is "emptied". The storage duration of the local vector
is already over. The vector doesn't exist anymore. That's why your code
produces undefined behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top