C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Slightly Misleading Example?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Tomás
Guest





PostPosted: Wed May 10, 2006 10:21 pm    Post subject: Slightly Misleading Example? Reply with quote



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






PostPosted: Thu May 11, 2006 2:21 am    Post subject: Re: Slightly Misleading Example? Reply with quote



"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.