|
-
April 21st, 2010, 11:54 AM
#1
Curiosity about "template-ready" containers
I read that a "template-ready" class must include a default constructor, a copy constructor and a = operator overload (information taken from http://www.codeproject.com/KB/stl/Pr...lGuideStl.aspx)
But I have used this class:
class MyPoint
{
public:
//! Coordinates of the point
float x,y;
//! Data
float data[90];
};
as a container (a defined type to be used in declarations like vector <MyPoint> m_var) without problems.
Why is that?
Maybe just if I wanted to make a complete class (with = operator support and things like that) I had to declare it in that way?
Just a doubt though
-
April 21st, 2010, 12:06 PM
#2
Re: Curiosity about "template-ready" containers
Actually, this is C-like structure, with public members and without any methods. Default constructor, copy constructor and operator= are provided by compiler. This class is ready to use in any container. You only need to think about performance, because when such class instance is added to container, the whole data array is copied.
Default copy constructor and operator= just copy all class members between instances. It is OK if you don't use dynamically allocated data in the class.
Last edited by Alex F; April 21st, 2010 at 12:08 PM.
-
April 21st, 2010, 12:07 PM
#3
Re: Curiosity about "template-ready" containers
 Originally Posted by leKoxn
I read that a "template-ready" class must include a default constructor, a copy constructor and a = operator overload
I think that by "template-ready" the article really means "suitable to be stored in a standard container". Note that the default constructor is not always required, e.g., you can construct a std::vector of objects of a type that cannot be default constructed.
 Originally Posted by leKoxn
But I have used this class: (...) as a container (a defined type to be used in declarations like vector <MyPoint> m_var) without problems.
That is using a container of objects of the class, not using the class as a container. You should be aware that if they are required but not explicitly provided, the compiler will generally implicitly define the default constructor, copy constructor, copy assignment operator and destructor for the class.
-
April 21st, 2010, 03:43 PM
#4
Re: Curiosity about "template-ready" containers
Thank you, nothing more than a class-copy in case of use of the = operator, right?
This sounds insane for most of C++ code, but is to blame in this case where little data is present into the class too?
I mean: are you suggesting to add such constructors and = operator overload also for this little structure/class?
-
April 21st, 2010, 05:43 PM
#5
Re: Curiosity about "template-ready" containers
 Originally Posted by leKoxn
I mean: are you suggesting to add such constructors and = operator overload also for this little structure/class?
You can/should add a default constructor to initialize the members, but you shouldn't code copy constructor, assignment op, or destructor for such a simple class.
The compiler provided version works perfectly, so there is no need to write your own.
Regards,
Paul McKenzie
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
|