Topic: Slightly Misleading Example?


Author: NULL@NULL.NULL ("Tom s")
Date: Wed, 10 May 2006 21:56:08 GMT
Raw View
On page 22 of the Standard, it gives the following example of a structure=
:

struct C {
    string s;    // string is the standard library class (clause 21)
};


And then it goes on to say that this is equivalent to:

struct C {
    string s;

    C(): s() { }

    C(const C& x): s(x.s) { }

    C& operator=3D(const C& x) { s =3D x.s; return *this; }

    =98C() { }
};


While this is true, I think it's misleading as the example only works=20
because "s" is a user-defined class which has a constructor. I think it'd=
 be=20
better to change the constructor definition to:

    C() {}

This is "more correct" because it now works with a primitive type, e.g.:

struct C {
    int s;
};

struct C {
    int s;

    C() { }  /* s doesn't get default initialised */

    C(const C& x): s(x.s) { }

    C& operator=3D(const C& x) { s =3D x.s; return *this; }

    =98C() { }
};


-Tom=E1s

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: johnchx2@yahoo.com
Date: Wed, 10 May 2006 20:54:09 CST
Raw View
"Tom   s" wrote:

> While this is true, I think it's misleading as the example only works
> because "s" is a user-defined class which has a constructor. I think it'd be
> better to change the constructor definition to:
>
>     C() {}
>

It's also a better match to the language in 12.1/7, which says, "The
implicitly-defined default constructor performs the set of
initializations of the class that would be perfomed by a user-written
default constructor for that class with an empty mem-initializer-list
(12.6.2) and an empty function body."


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]