 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tomás Guest
|
Posted: Wed May 10, 2006 10:21 pm Post subject: Slightly Misleading Example? |
|
|
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=(const C& x) { s = x.s; return *this; }
˜C() { }
};
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() {}
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=(const C& x) { s = x.s; return *this; }
˜C() { }
};
-Tomás
---
[ 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 ] |
|
| Back to top |
|
 |
Guest
|
Posted: Thu May 11, 2006 2:21 am Post subject: Re: Slightly Misleading Example? |
|
|
"Tomás" wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|