473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.

Mar 30 '06 #1
16 1956

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

Mar 30 '06 #2
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 .

Mar 30 '06 #3

海风 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=(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

Mar 30 '06 #4
"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."
Mar 30 '06 #5

"osmium" <r1********@comcast.net> schrieb im Newsbeitrag
news:49************@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.
Mar 30 '06 #6
In message <49************@individual.net>, Gernot Frisch
<Me@Privacy.net> writes

"osmium" <r1********@comcast.net> schrieb im Newsbeitrag
news:49************@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
Mar 30 '06 #7
yes , Gavin is right. So I suggest that you implement constructor and
destructor at the same time or nothing.

Mar 31 '06 #8
海风 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...
Mar 31 '06 #9
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

Mar 31 '06 #10
In article <11**********************@u72g2000cwu.googlegroups .com>,
"Ajay" <aj**********@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.
Apr 1 '06 #11
* Daniel T.:
In article <11**********************@u72g2000cwu.googlegroups .com>,
"Ajay" <aj**********@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?
Apr 1 '06 #12
In article <49************@individual.net>,
"Alf P. Steinbach" <al***@start.no> wrote:
* Daniel T.:
In article <11**********************@u72g2000cwu.googlegroups .com>,
"Ajay" <aj**********@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.
Apr 1 '06 #13
* Daniel T.:
In article <49************@individual.net>,
"Alf P. Steinbach" <al***@start.no> wrote:
* Daniel T.:
In article <11**********************@u72g2000cwu.googlegroups .com>,
"Ajay" <aj**********@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?
Apr 1 '06 #14
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/e..._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/e...ta_frames.html.

Apr 2 '06 #15
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.

Apr 3 '06 #16

us****@aristeia.com 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/e..._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.

Apr 3 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ganesh J. Acharya | last post by:
Hi Friends! I had posted a query in the comp.infosystems.www.authoring.html group > Thanks for your guidance. Please check for the document now. I have > once again updated the website.... > >...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
26
by: Lasse Edsvik | last post by:
Hello I'm trying to build a simple COM+ app in vs.net using C# and i cant register it in component manager..... what more is needed than this: using System; using...
7
by: MrNobody | last post by:
I was a Java developer so I'm used to using property files as a means to keep configuration settings for my apps. I'm wondering what options are there with ..NET? Some settings I want to include...
4
by: Rob Meade | last post by:
Hi all, I'm trying to dynamically create the navigation for my web page - I have achieved this at work and whilst trying to reproduce the code this evening at home I'm obviously missing...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
5
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
6
by: Tony Johansson | last post by:
Hello!! If you write private class Test {} you have a private class If you write public class Test {} you have a public class If you write protected class Test {} you have a protected class If...
10
by: Simon Brooke | last post by:
The DOM API has included public Node importNode(Node,boolean) as a method of the Document interface for a long time. Does anything actually implement it? Xerces 2 is giving me: ...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.