Thanks for all the replies!
Quote:
Yes; that's what .push_back() is for.
Maybe I should have bin more specific in my question, however what I wanted to find out, was whether .push_back() would only put the reference in the vector and not copy the object. Meaning when the
Myclass t(...)
would go out of scope and be deleted, the reference would be invalid. But I probably should have thought about it one extra time, before asking, since
classes is a vector of
MyClass and not
MyClass references.
Quote:
However, do you really need to "do stuff with t" before putting it into the vector? What is "stuff" here?
"Stuff" in this case is to initialize a member of MyClass with a random number. Now, this could be done in a constructor, but it is not really coherent with the class interface - using a setter method would be better in this case.
Another question I have;
When declaring members in the header file like this:
class MyClass {public: MyClass(); virtual ~MyClass();private: MyOtherClass o;};
I noticed that the default (MyOtherClass(void)) constructor is automatically called. I looked it up and it is an error not to initialise the object when declared, thus the automatic call to the default constructor. BUT what I want, and this is most likely a Java ting I shouldn't be doing in C++, is to just declare the member object (o), and later initialise it:
o = MyOtherClass(...);
The reason for initialising it later could be that the constructor arguments are not calculated when the header is loaded. So the question is what is the correct C++ way to do this ? Right now I just have a default constructor that does nothing and then later reassign the member variable.