-
July 1st, 2004, 08:41 PM
#1
std::Vector push_back
i want to know what happens when push_back is called?
Code:
class A
{
public:
int n;
int *pn;
public:
A(void)
{
n = 10;
pn = new int[n];
}
~A(void){ delete [] pn; }
};
vector<A> B;
A a;
B.reserve(10);
B.clear();
B.push_back(a);
do i need to override the operator=?
-
July 1st, 2004, 09:03 PM
#2
For your class yes, since your class is not copyable by default.
A class is copyable with the auto-generated copy constructor if it only contains PODs (plain old datatypes like int, double and char) or copyable objects. A pointer to memory is not in there so you'll have to define your own copy constructor and assignment operator. In general it's a very good indication that if you need a destructor, you also need explicit copy semantics.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
July 1st, 2004, 11:30 PM
#3
thanks Yves M for so quick reply!
i want to know if i dont define copy contructor, what will be happen when calling push_back and pop_back.
and can you give a easy example to show how to define a class with copy constructor and operator= . and what is basic rules to define a class with more than POD data types? sorry for my ignorance, i seldem define copy constructor though i know maybe i should define it, because i dont know in which circumstance i should define it. and i seldem override operator= for the same reason. can you give me a elegant way to solve the problem,thanks.
-
July 2nd, 2004, 12:47 AM
#4
yeah,you can define a copy constructor in this way:
inside the class
classname (classname&objectname);
or outside the class
classname::classname(classname&objectname)
{
implementation
}
its main use is to cpoy old objects to new objects
as for opertor = it has something with the polymorphism
it is a way of polymorphism
you can define it in this way :
opertaor =(class/struct/union objectname) and it targets the class/struct/union object .it provides conveince for operations in the main function ,it cannot run pod data.
-
July 2nd, 2004, 12:59 AM
#5
yeah ,you can use it in this way :
copy constructor
inside the class
classname (classname&objectname)
outside the class
classname::classname(classname&objectname)
its main advantage is to use the old objects to give value to new objects .
opertor =
it is a way of polymorphism it often treat class/struct/union object
the parameter is not pod,and it provides conveince for the main function
-
July 2nd, 2004, 01:04 AM
#6
Originally posted by fantasy2004
i want to know if i dont define copy contructor, what will be happen when calling push_back and pop_back.
Well...the problem will occur while destroying the classes...as soon as your class will be copy-constructed or assigned, the pointer to your dynamically allocated memory will simply be copied...thus, resulting in two or more class instances pointing to the same allocated memory...if every instance tries to delete the memory...crash...boom...bang...
Originally posted by fantasy2004
and can you give a easy example to show how to define a class with copy constructor and operator= . and what is basic rules to define a class with more than POD data types?
By default the copy constructor and assignment operator are making so-called shallow copies of the object meaning that all of the member values will be copied. This works well if the members are values, but causes problems for members which actually points to dynamically allocated memory. The pointer will be copied - but not the memory it points to...this will result in having both members pointing to the same dynamically allocated memory.
Therefore, if you have a class with dynamic memory allocation a deep copy is required to guarantee that all members gets copied properly. To make a deep copy, you must write a copy constructor and overload the assignment operator.
Code:
class CFoo()
{
public:
// Constructor
CFoo() { m_pArray = new char[100]; }
// Destructor
~CFoo() { delete [] m_pArray; }
// Copy constructor
CFoo(const CFoo &refcSource)
{
// Allocate new space
m_pArray = new char[strlen(refcSource.m_pArray) + 1];
// Copy values
strcpy(m_pArray, refcSource.m_pArray);
}
// Assignment operator
CFoo& operator=(const CFoo &refcSource)
{
// Check whether it is the same instance
if(&refcSource != this)
{
// Release old memory
delete [] m_pArray;
// Allocate new space
m_pArray = new char[strlen(refcSource.m_pArray) + 1];
// Copy values
strcpy(m_pArray, refcSource.m_pArray);
}
return *this;
}
private:
char *m_pArray;
};
-
July 2nd, 2004, 01:07 AM
#7
Originally posted by jolley
classname (classname&objectname)
It should rather be
Code:
classname (const classname& rhs)
Originally posted by jolley
opertor =
it is a way of polymorphism it often treat class/struct/union object
the parameter is not pod,and it provides conveince for the main function
Well..the assignment operator has nothing to do with polymorphism...but simply is a different way of creating an instance of the class (other than construct or copy-construct)...
-
July 2nd, 2004, 01:24 AM
#8
thanks Andreas Masur
you teach me a lot !
-
July 2nd, 2004, 02:05 AM
#9
the copy constructor is needn't...
the copy constructor is needn't, if you save the pointer in the vector only .
vector<A*> vtr;
A *pA = new A();
vtr.push_back(pA); //It's safety!
remember, you need to free the memory by yourself:
vector<A*>::iterator it = vtr.begin();
for(;it!=vtr.end();it++)
{
delete *it; //(*it)==pA
}
Last edited by SkyMountain; July 2nd, 2004 at 02:20 AM.
-
July 2nd, 2004, 05:51 AM
#10
thanks jolley,Andreas Masur and SkyMountain:
you teach me a lot !
-
July 2nd, 2004, 06:01 AM
#11
Originally posted by fantasy2004
thanks Yves M for so quick reply!
i want to know if i dont define copy contructor, what will be happen when calling push_back and pop_back.
You're concentrating too much on what a vector will do with your object. It isn't vector that is the problem -- a simple program that doesn't use vector will cause the same problems:
Code:
int main()
{
A A1;
A A2 = A1; // copy constructor (problem)
A A3;
A3 = A1; // assignment - another problem
} // destructor for A1, A2, and A3, yet another problem
Your class doesn't even work for the code I have above. There is no vector at all in the code I posted. This is why you define a copy constructor and assignment operator (and destructor), so that simple operations such as I have above work correctly.
Regards,
Paul McKenzie
-
July 2nd, 2004, 06:01 AM
#12
Just for completeness:
Code:
class foo
{
public:
foo(foo&);
foo(const foo&);
foo(volatile foo&);
foo(const volatile foo&);
};
are all valid copy constructors...
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 2nd, 2004, 07:16 AM
#13
thanks Paul McKenzie
you are absolutely right. can you give me some constructive suggestion such as the common rules to deal with this kind of problem, i can read c++ codes and try to understand it step by step, but i lack the ability to write it on my own.
and thanks Graham.
Code:
class foo
{
public:
foo(foo&);
foo(const foo&);
foo(volatile foo&);
foo(const volatile foo&);
};
i dont very sure about the "Just for completeness".
do you mean it is just for completeness in theroy and it is not practical?
or it is complete in every circumstance?
and i want to know what others things i should do besides copy constructor?
thanks for your kindness!
-
July 2nd, 2004, 07:28 AM
#14
Originally posted by fantasy2004
thanks Paul McKenzie
you are absolutely right. can you give me some constructive suggestion such as the common rules to deal with this kind of problem, i can read c++ codes and try to understand it step by step, but i lack the ability to write it on my own.
Andreas Masur already did that for you. He spelled out exactly what you need to do: write a copy constructor and overload the assignment operator for your class.
-
July 2nd, 2004, 07:40 AM
#15
thanks Bob Davis
Andreas Masur already did that for you. He spelled out exactly what you need to do: write a copy constructor and overload the assignment operator for your class.
i want to know what others things i should do besides copy constructor and assignment operator?
or in other words, it's enough to write a copy constructor and overload the assignment operator for your class?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|