C
Charles Herman
I am using push_back to place a class object into a vector. This class
contains data members that are pointers to a char string. When I use
push_back, I believe a copy of the object is first made, and then deposited
into the vector.
But I want this copy to not mereley copy the pointer to the char string, but
rather, I want to malloc new space, and make a copy of the char string.
The default copy constructor is defined as
MyClass( const MyClass& oldObject ).
At this stage, my method that defines the copy constructor would copy the
char string from oldObject to the new object. With push_back I can't do
this. The only way I can think of to do this, is to make two copies of the
object, one for the purpose of using the above copy constructor, and a
second when it is push_back'd to the vector.
This can be rather expensive. Is there a way I can achieve what I want and
avoid doing duplicat work?
-charles
I might be able to use the following syntax:
push_back(MyClass( oldObject )).
Will this work, and will it avoid making two copies?
contains data members that are pointers to a char string. When I use
push_back, I believe a copy of the object is first made, and then deposited
into the vector.
But I want this copy to not mereley copy the pointer to the char string, but
rather, I want to malloc new space, and make a copy of the char string.
The default copy constructor is defined as
MyClass( const MyClass& oldObject ).
At this stage, my method that defines the copy constructor would copy the
char string from oldObject to the new object. With push_back I can't do
this. The only way I can think of to do this, is to make two copies of the
object, one for the purpose of using the above copy constructor, and a
second when it is push_back'd to the vector.
This can be rather expensive. Is there a way I can achieve what I want and
avoid doing duplicat work?
-charles
I might be able to use the following syntax:
push_back(MyClass( oldObject )).
Will this work, and will it avoid making two copies?