By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,759 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,759 IT Pros & Developers. It's quick & easy.

why vector assignment operator not invoked on const vector& ?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"zhou" <yz***@medplus.com> wrote in message news:bj************@ID-171337.news.uni-berlin.de...
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.
Jul 19 '05 #2

P: n/a


zhou wrote:

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).
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3

P: n/a
zhou wrote:
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.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #4

P: n/a

"zhou" <yz***@medplus.com> wrote in message
news:bj************@ID-171337.news.uni-berlin.de...
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
Jul 19 '05 #5

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bj************@ID-196037.news.uni-berlin.de...

"zhou" <yz***@medplus.com> wrote in message
news:bj************@ID-171337.news.uni-berlin.de...
I see. However, why the copy constructor does not copy the vector?

Yan


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


Might have been too hasty with my last post. I think I'll bow out now.

john
Jul 19 '05 #6

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bj************@ID-196037.news.uni-berlin.de...

I think you should post some real.


code.
Jul 19 '05 #7

P: n/a
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.
Jul 19 '05 #8

P: n/a
zhou wrote:
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.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #9

P: n/a
zhou wrote:
I see.
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.