what all a class has by default



 DEVELOP > c-Plus-Plus > what all a class has by default

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Ajay"
Date: 30 Mar 2006 03:08:10 AM
Object: what all a class has by default
Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.
1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator
Thanks in advance.
.

User: "osmium"

Title: Re: what all a class has by default 30 Mar 2006 07:14:40 AM
"Ajay" writes:

i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator

Your list is complete, there ain't no more. The implicit copy constructor,
especially, can really spoil your day if you are a novice. Because a novice
may not be aware that he is even invoking the copy constructor, much less
realize that the CC does not do what he needs to have done. It might be
nice to have a C++ compiler with training wheels that emits the following
message:
"Warning! You have implicitly called the copy constructor. You may be
sorry."
.
User: "Gernot Frisch"

Title: Re: what all a class has by default 30 Mar 2006 07:41:41 AM
"osmium" <r124c4u102@comcast.net> schrieb im Newsbeitrag
news:4923u1Fm41vjU1@individual.net...

"Ajay" writes:

i want to know when i create a class.what all it
contains.I
know the following things are there by default if i do not declare
them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator


Your list is complete, there ain't no more. The implicit copy
constructor, especially, can really spoil your day if you are a
novice. Because a novice may not be aware that he is even invoking
the copy constructor, much less realize that the CC does not do what
he needs to have done. It might be nice to have a C++ compiler with
training wheels that emits the following message:

"Warning! You have implicitly called the copy constructor. You may
be sorry."

Even better: The standart should tell the default CC to use the
(default or not) = operator.
.
User: "Richard Herring"

Title: Re: what all a class has by default 30 Mar 2006 08:44:25 AM
In message <4925chFmdtfgU1@individual.net>, Gernot Frisch
<Me@Privacy.net> writes


"osmium" <r124c4u102@comcast.net> schrieb im Newsbeitrag
news:4923u1Fm41vjU1@individual.net...

"Ajay" writes:

i want to know when i create a class.what all it
contains.I
know the following things are there by default if i do not declare
them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator


Your list is complete, there ain't no more. The implicit copy
constructor, especially, can really spoil your day if you are a
novice. Because a novice may not be aware that he is even invoking
the copy constructor, much less realize that the CC does not do what
he needs to have done. It might be nice to have a C++ compiler with
training wheels that emits the following message:

"Warning! You have implicitly called the copy constructor. You may
be sorry."


Even better: The standart should tell the default CC to use the
(default or not) = operator.

Definitely not. The semantics of copy and assign are quite different:
operator= has to clean up the old value before assigning the new one,
while the copy constructor starts with a blank slate
And if operator= is defined (as is common because of this) in terms of
copy and swap, you're inviting infinite recursion.
--
Richard Herring
.


User: "Jaspreet"

Title: Re: what all a class has by default 02 Apr 2006 07:32:52 PM
osmium wrote:

"Ajay" writes:

i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator


Your list is complete, there ain't no more. The implicit copy constructor,
especially, can really spoil your day if you are a novice. Because a novice
may not be aware that he is even invoking the copy constructor, much less
realize that the CC does not do what he needs to have done. It might be
nice to have a C++ compiler with training wheels that emits the following
message:

"Warning! You have implicitly called the copy constructor. You may be
sorry."

The list broadly covers what a compiler provides by default for a
class. (That may or may not be what you would really want, as mentioned
in the above post).
However, do we also need to include the & (address of operator) in the
list to be one of the operators which we can use on a class without
explicitly defining it.
For instance..
class myEx {
-----
};
-----
int main() {
myEx *pEx;
myEx aEx;
pEx = &aEx;
-----
}
Please let me know.
.


User: "Gavin Deane"

Title: Re: what all a class has by default 30 Mar 2006 03:25:20 AM
Ajay wrote:

Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor

You need to be more specific with this one. The compiler will generate
a default constructor (that is, a constructor called with no arguments)
_only_ if you do not declare _any_ constructors.

2.Destructor
3.Copy constructor
4. Assignment operator

Correct. For each of those, if you haven't declared it the compiler
will generate it for you. The "Rule of Three" says that if you need to
define your own implementation of one of those three because the
compiler generated one does the wrong thing, you probably need to
define all three.
Gavin Deane
.
User: "=?utf-8?B?5rW36aOO?="

Title: Re: what all a class has by default 30 Mar 2006 04:05:00 AM
If you do not implement your own Destructor, a compiler maybe generates
one automatically or generates nothing,. BUT it does not generate one
does the wrong thing .
.
User: "Gavin Deane"

Title: Re: what all a class has by default 30 Mar 2006 04:27:26 AM
=E6=B5=B7=E9=A3=8E wrote:

If you do not implement your own Destructor, a compiler maybe generates
one automatically or generates nothing,. BUT it does not generate one
does the wrong thing .

Well I suppose that depends on what one means by "wrong", so let me
clarify.
A conforming compiler will, of course, generate a destructor that does
exactly what the standard says compiler-generated destructors do. So in
that sense, the destructor is not doing the "wrong" thing.
However, given this class
#include <string>
class foo
{
public:
foo() : pstr(new std::string("Leak")) {}
private:
// Let's just disable these
foo(const foo&);
foo& operator=3D(const foo&);
std::string* pstr;
};
the compiler-generated destructor will not delete the string allocated
in the constructor and so every object of foo type will leak memory
when it is destroyed. This is (presumably) not the behaviour you want
so you need to write the destructor yourself. That's what I mean by the
compiler-generated destructor doing the "wrong" thing. It does exactly
what the standard says it should do, but I need it to do something
else.
Gavin Deane
.
User: "=?utf-8?B?5rW36aOO?="

Title: Re: what all a class has by default 31 Mar 2006 03:31:29 AM
yes , Gavin is right. So I suggest that you implement constructor and
destructor at the same time or nothing.
.
User: "Ben Pope"

Title: Re: what all a class has by default 31 Mar 2006 04:05:29 AM
海风 wrote:

yes , Gavin is right. So I suggest that you implement constructor and
destructor at the same time or nothing.

And if you implement a destructor, you need to either implement the
assignment operator and copy constructor, or disable them.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
.
User: "Diego Martins"

Title: Re: what all a class has by default 31 Mar 2006 12:35:28 PM
if you implement ANY constructor, the default constructor is not
generated
at first sight, it seems quite irrelevant, but I had troubles when i
only implemented the COPY constructor
I had to create an empty default constructor because my compiler did
not generate one in this case
.






User: "Daniel T."

Title: Re: what all a class has by default 31 Mar 2006 05:50:48 PM
In article <1143709690.868142.310590@u72g2000cwu.googlegroups.com>,
"Ajay" <ajay.kumarCS@gmail.com> wrote:

Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator

Thanks in advance.

From Scott Meyers' book:
.... if you write this:
class Empty { };
it's the same as if you'd written this:
class Empty {
public:
Empty() { }
Empty( const Empty& rhs );
~Empty() { }
Empty& operator=( const Empty& rhs );
Empty* operator&() { return this; }
const Empty* operator&() const { return this; }
};
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
.
User: "Alf P. Steinbach"

Title: Re: what all a class has by default 31 Mar 2006 06:02:09 PM
* Daniel T.:

In article <1143709690.868142.310590@u72g2000cwu.googlegroups.com>,
"Ajay" <ajay.kumarCS@gmail.com> wrote:

Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator

Thanks in advance.


From Scott Meyers' book:

... if you write this:

class Empty { };

it's the same as if you'd written this:

class Empty {
public:
Empty() { }
Empty( const Empty& rhs );
~Empty() { }
Empty& operator=( const Empty& rhs );
Empty* operator&() { return this; }
const Empty* operator&() const { return this; }
};

Uhuh. Did Scott Meyer really write that?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "Daniel T."

Title: Re: what all a class has by default 31 Mar 2006 06:46:26 PM
In article <495u4dFmkic2U1@individual.net>,
"Alf P. Steinbach" <alfps@start.no> wrote:

* Daniel T.:

In article <1143709690.868142.310590@u72g2000cwu.googlegroups.com>,
"Ajay" <ajay.kumarCS@gmail.com> wrote:

Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator

Thanks in advance.


From Scott Meyers' book:

... if you write this:

class Empty { };

it's the same as if you'd written this:

class Empty {
public:
Empty() { }
Empty( const Empty& rhs );
~Empty() { }
Empty& operator=( const Empty& rhs );
Empty* operator&() { return this; }
const Empty* operator&() const { return this; }
};


Uhuh. Did Scott Meyer really write that?

"Effective C++ Second Edition" by Scott Meyers, item 45.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
.
User: "Alf P. Steinbach"

Title: Re: what all a class has by default 31 Mar 2006 07:16:22 PM
* Daniel T.:

In article <495u4dFmkic2U1@individual.net>,
"Alf P. Steinbach" <alfps@start.no> wrote:

* Daniel T.:

In article <1143709690.868142.310590@u72g2000cwu.googlegroups.com>,
"Ajay" <ajay.kumarCS@gmail.com> wrote:

Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator

Thanks in advance.

From Scott Meyers' book:

... if you write this:

class Empty { };

it's the same as if you'd written this:

class Empty {
public:
Empty() { }
Empty( const Empty& rhs );
~Empty() { }
Empty& operator=( const Empty& rhs );
Empty* operator&() { return this; }
const Empty* operator&() const { return this; }
};

Uhuh. Did Scott Meyer really write that?


"Effective C++ Second Edition" by Scott Meyers, item 45.

Well, if that's correct, then the good Scott Meyers is teaching Wrong
Things (TM). There is no automatically generated address operator
member function, and the two classes above are /not/ equivalent. That
means, you can do things with one that you can't with the other:
class Empty1 {};
class Empty2 {
public:
Empty2() { }
Empty2( const Empty2& rhs );
~Empty2() { }
Empty2& operator=( const Empty2& rhs );
Empty2* operator&() { return this; }
const Empty2* operator&() const { return this; }
};
int main()
{
// OK
Empty2& (Empty2::*a)( Empty2 const& ) = &Empty2::operator=;
Empty2 const* (Empty2::*b)() const = &Empty2::operator&;
Empty2* (Empty2::*c)() = &Empty2::operator&;
// OK
Empty1& (Empty1::*d)( Empty1 const& ) = &Empty1::operator=;
// ERROR:
Empty1 const* (Empty1::*e)() const = &Empty1::operator&;
// ERROR:
Empty1* (Empty1::*f)() = &Empty1::operator&;
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.



User: ""

Title: Re: what all a class has by default 02 Apr 2006 01:28:26 PM
Daniel T. wrote:

From Scott Meyers' book:

... if you write this:

class Empty { };

it's the same as if you'd written this:

class Empty {
public:
Empty() { }
Empty( const Empty& rhs );
~Empty() { }
Empty& operator=( const Empty& rhs );
Empty* operator&() { return this; }
const Empty* operator&() const { return this; }
};

Ah, but from the errata list for that book
(http://www.aristeia.com/BookErrata/ec++2e-errata_frames.html):
A class declaring no operator& function(s) does NOT have them
implicitly declared. Rather, compilers use the built-in address-of
operator whenever "&" is applied to an object of that type. This
behavior, in turn, is technically not an application of a global
operator& function. Rather, it is a use of a built-in operator.
I eliminated mention of operator& as an automatically generated
function and adjusted the index to eliminate entries for the removed
material.
This was fixed in September 2001. I apologize for getting it wrong in
the first place.
I can't help but remark that I encourage you to take a look at the
third edition of the book, which came out about a year ago. Not only
does it not contain errors such as this (*), it includes lots of new
information that I believe is essential for effective C++ programming,
e.g., using objects to manage resources, writing exception-safe code,
implementing traits classes, etc.
Scott
(*) It contains different kinds of errors, sigh. For the current
list, check out
http://www.aristeia.com/BookErrata/ec++3e-errata_frames.html.
.
User: "Jaspreet"

Title: Re: what all a class has by default 02 Apr 2006 08:20:36 PM
wrote:

Daniel T. wrote:

From Scott Meyers' book:

... if you write this:

class Empty { };

it's the same as if you'd written this:

class Empty {
public:
Empty() { }
Empty( const Empty& rhs );
~Empty() { }
Empty& operator=( const Empty& rhs );
Empty* operator&() { return this; }
const Empty* operator&() const { return this; }
};


Ah, but from the errata list for that book
(http://www.aristeia.com/BookErrata/ec++2e-errata_frames.html):

A class declaring no operator& function(s) does NOT have them
implicitly declared. Rather, compilers use the built-in address-of
operator whenever "&" is applied to an object of that type. This
behavior, in turn, is technically not an application of a global
operator& function. Rather, it is a use of a built-in operator.

I eliminated mention of operator& as an automatically generated
function and adjusted the index to eliminate entries for the removed
material.

This was fixed in September 2001. I apologize for getting it wrong in
the first place.

I can't help but remark that I encourage you to take a look at the
third edition of the book, which came out about a year ago. Not only
does it not contain errors such as this (*), it includes lots of new
information that I believe is essential for effective C++ programming,
e.g., using objects to manage resources, writing exception-safe code,
implementing traits classes, etc.

Scott

Thanks for the errata link. I stand corrected that & operator is not
automatically generated.
.




  Page 1 of 1

1

 


Related Articles
Question about pointer to class that has dynamic memory
why empty class has size 1 byte
Changing which methods a class has by deriving from a template argument
How can I use a class name Rect when there is a function has the same name
Re: Copy assignment for derived class when base class has privatemembers
Help! Newbie to MS Visual C++.NET has Class Wizard problem
Re: Copy assignment for derived class when base class has private members
anyone who has Converted COBOL program to C/C++(mainframe)
Has anyone tried my reference counting algorithm?
i think this code has a logical error ..plz help me...
Format of compiler generated derived destructor when base has 'virtual ~base() throw():"
Does filename has to be the same as funtion name??
Is it possible to find in what language a dll has been programmed?
Does anyone has already seen a non mutable String based on std::string
C and only C language has a standard 64 bit integer type ?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER