-1

There are many SO questions on closely related topics, but I coudn't find one which exactly addresses my problem.

Given:

std::vector<int> a, b;

what's the difference between the following two cases:

std::vector<int> c(a); // case c(a)
std::vector<int> d = a; // case d=a

and also later:

c(b); // c(b): does this make sense?
c = b; // or is this better? and if so, why?
That
  • 25
  • 3
Daniel S.
  • 6,458
  • 4
  • 35
  • 78
  • So the answer is that copy-elision and return value optimization are the keys for judging if I shall use c(b) or c = b?! I've never heard these terms before. How the heck am I supposed to find that out otherwise if not asking? – Daniel S. Aug 04 '15 at 18:17
  • @LightnessRacesinOrbit The downvoters. The question might have some other weaknesses, but I presume it was downvoted because most people think that it's easy to find the questions which I duplicated. – Daniel S. Aug 04 '15 at 18:54
  • Well you know what "presuming" does :) – Lightness Races in Orbit Aug 04 '15 at 18:57
  • 2
    I have no idea why someone selected that as a duplicate. I'm sure there is a duplicate somewhere, but it's certainly not a duplicate of a question about copy elision. – Benjamin Lindley Aug 04 '15 at 18:59
  • Voting to reopen, as while the marked duplicate question might be related, OP is asking a different question, and none of the answers in the duplicate directly answer it. – Maximillian Laumeister Aug 04 '15 at 19:00

1 Answers1

-2

C(b) is best way... This will call the constructor which means your object will get created constructed before you started using it.

Best way is to understand this is, if you create const object of c second approach will give error saying object is read only, while first one will work.

Swapnil
  • 1,424
  • 2
  • 19
  • 30