CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    23

    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

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Curiosity about "template-ready" containers

    Quote 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.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Aug 2009
    Posts
    23

    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?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Curiosity about "template-ready" containers

    Quote Originally Posted by leKoxn View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured