Difference on std::vector push_back(Object()) and push_back(new Object())?

21,130

Solution 1

First one adds non-pointer object, while the second one adds pointer to the vector. So it all depends on the declaration of the vector as to which one you should do.

In your case, since you have declared objects as std::vector<DrawObject>, so the first one will work, as objects can store items of type DrawObject, not DrawObject*.

In C++11, you can use emplace_back as:

objects.emplace_back(name, surfaceFile, xPos, yPos, 
                     willMoveVar, animationNumber);

Note the difference. Compare it with:

objects.push_back(DrawObject(name, surfaceFile, xPos, yPos, 
                            willMoveVar, animationNumber));

With emplace_back, you don't construct the object at the call-site, instead you pass the arguments to vector, and the vector internally constructs the object in place. In some cases, this could be faster.

Read the doc about emplace_back which says (emphasize mine),

Appends a new element to the end of the container. The element is constructed in-place, i.e. no copy or move operations are performed. The constructor of the element is called with exactly the same arguments that are supplied to the function.

As it avoids copy or move, the resultant code could be a bit faster.

Solution 2

Given

std::vector< DrawObject > objects;

The difference between the two versions is that the first one is correct, and the second one isn't.

If the second version compiles, as you indicate in the comments, it doesn't do what you expect it might. It also suggests that you should probably consider making some of DrawObject's constructors explicit.

Solution 3

The second version expects objects to be a vector of pointers to DrawObject, while the first one expects objects to hold the objects themselves. Thus depending on the declaration of objects only one of the versions will compile.

If you declare objects the way you did, only the first version will compile, if you declare objects as std::vector< DrawObject*> objects; only the second version will compile.

Share:
21,130
parreirat
Author by

parreirat

6 years total on-the-job experience as a Fullstack Web Developer (Backend, Frontend, DevOps, anything that needs doing) 2 years in a technlogical startup, Passworks, working with mobile wallets. 1 year in Runtime Revolution, an in-house Rails consultancy shop: working in teams remotely to projects all over the world. And now 3 years as a senior fullstack engineer in HypoFriend, helping people make smart homebuying decisions for their specific situations, through leveraging the power of technology, mathematics &amp; financial modeling. Currently looking for a job, reach out if interested :) What I would like: Small team. Team-oriented colleagues, no one-man-genius gigs. Having a say in how things are done and which direction the product takes. Freedom to try new technologies, bonus if being incentivized towards doing it. Startupish environment (the above). Flexible hours, possibility of working remotely occasionally or full time. Having a physical office in Berlin; What I am: Outside the box thinker. Hard working, motivated. A very quick learner: throw me whatever new technology you'd think is useful. An eager learner: I want to know more, and better. Very social, friendly person. A good teacher, and a great listener. Inquisitive: I won't take your word (whoever you are, a junior, senior, CTO or president) as fact just because of your standing. Email me on [email protected] if you'd like to have a chat! Bonus: I think it's a far off shot but I love Game Development: have done quite a few little projects 4 years back and even participated solo in a Game Jam called Ludum Dare. Have used several libraries: SDL, SFML, LibGDX, jMonkeyEngine, and a few small others, spread between C, C++, Java, Ruby, and Javascript. Would love an Entry level Game Developer position: do contact me!

Updated on January 06, 2020

Comments

  • parreirat
    parreirat over 4 years

    In my current code, I'm wanting to insert new DrawObjects into a vector I created,

    std::vector< DrawObject > objects;

    What is the difference between:

    objects.push_back(DrawObject(name, surfaceFile, xPos, yPos, willMoveVar, animationNumber));
    

    and

    objects.push_back(new DrawObject(name, surfaceFile, xPos, yPos, willMoveVar, animationNumber));