tags:

views:

80

answers:

3

if for example I do:

FOO foo;
foovect.push_back(foo);

where FOO is a class with a default constructor.

Will the constructor be called only when foo is put on the stack, or is it called again when pushed into the std::vector?

Thanks

I'm doing:

OGLSHAPE::OGLSHAPE(void)
{
    glGenBuffersARB(GL_ARRAY_BUFFER_ARB,&ObjectVBOInt);

}
+3  A: 

No, the copy constructor is used, i.e. the one that looks like this:

FOO( const FOO & f );

A default copy constructor is provided by the compiler, if you don't provide one yourself.

anon
So the constructor will not be called twice?So if my constructor displays a message box I would only end up seeing 1 message box?
Milo
@user Once to construct the object (in your code) called "foo" and once to make the copy in the vector.
anon
@user146780: what do you mean, "the" constructor? There's a default constructor, a copy constructor, and maybe more.
JWWalker
Well let's say I call GlGenBuffers() which allocates a buffer object, would it end up generating 2 buffers to the graphics card?
Milo
@user: Depends on how "a buffer object" is defined.
FredOverflow
@user A constructor invocation only ever creates one object of the type it is a constructor for, at your command. I suspect you really don't understand the basic idea of constructors - which C++ textbook are you using?
anon
I guess the deconstructor for the one on the stack would be called so I guess it's alright.
Milo
See my edit to see what im doing
Milo
@user: Is `FOO` the same as `OGLSHAPE`? Who is responsible for freeing the buffer? Does `OGLSHAPE` have a destructor, copy constructor and assignment operator?
FredOverflow
It has a deconstructor to free the buffer
Milo
@user: Then that buffer will be freed when `push_back` returns. Probably not what you want...
FredOverflow
What I want is to create an OGLSHAPE, add some verticies (not with the constructor), push it into my vector, then update it from the vector when needed, then I want all resources to be freed when I remove it from the vector, would I be better to just use init() and deinit() functions?
Milo
@user: I suggest a vector of smart pointers to `OGLSHAPE`, for example `std::vector<boost::shared_ptr<OGLSHAPE> >` or `std::vector<std::unique_ptr<OGLSHAPE> >` if you have a C++0x compiler. That will save you a lot of pain.
FredOverflow
@JWWalker: There are more constructors! Along with the default constructor and the copy constructor there is also a parameterized constructor, conversion constructor and a sequence constructor. :D
Alerty
+3  A: 

FOO foo; would call the constructor.
foovect.push_back(foo); would call the copy constructor.

#include <iostream>
#include <vector>

class FOO
{
public:
    FOO()
    {
        std::cout << "Constructor" << std::endl;
    }
    FOO(const FOO& _f)
    {
        std::cout << "Copy Constructor" << std::endl;
    }
};

int main()
{
    FOO foo;
    std::vector<FOO> foovect;
    foovect.push_back(foo);
}

Output for this:
Constructor
Copy Constructor

rturrado
Perfect that clears it up, thanks
Milo
A: 

When you do a push_back, your object is copied into the vector. That means the copy constructor for your object gets called. All the standard library containers deal with copies of objects, not the object themselves. If you want that behavior, you'll need to resort to using pointers.

T.E.D.