Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using constructors in with vector::push_back()

Started by the_cyberlord Nov 20, 2004 at 2:37 AM 4 replies 900+ views
Original Post
the_cyberlord
the_cyberlord
Is it possible to use the class constructor with vectors? Like this: vector x; x.push_back(5,5.3,-5);
Tron3k
Tron3k
You can do this:
vector<coords> x;x.push_back(coords(5.0, 5.3, -5.0));

as long as you define the appropriate coords constructor. (Please note that this code also calls the copy constructor for coords. This isn't an issue for a small 3D point class, but for bigger classes it might be.)
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
the_cyberlord
the_cyberlord
I got the constructer right I think;
I'll try it now
robinei
robinei
Quote:
Original post by Tron3k
vector<coords> x;x.push_back(coords(5.0, 5.3, -5.0));


When you do something like that will a temporary object be created then copied, or will it be created in-place?
NoahAdler
NoahAdler
It will be created and copied. A temporary can only optimize out the copy operation when used in a function's return value, IIRC.
-bodisiw
Evil Steve
Evil Steve
Something I wondered about is, if theres no default constructor, no destructor, no copy constructor and no assignment operator (I.e. its a primitive type), then the compiler should be able to construct it in place. I don't know what the standards say, but if theres no code going to be called as a result of creating, copting and destroying, then it may as well be constructed in place.
I'd test this, but I have MSVC 6.0, and its not exactly good at optimizing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.