Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compiler-Generated Default Functions

Reply
Thread Tools

Compiler-Generated Default Functions

 
 
moleskyca1@yahoo.com
Guest
Posts: n/a
 
      07-25-2006
Hi,

In a recent discussion, some of us were in disagreement about the
functions the C++ compiler generates. How many functions are generated
by the compiler when you declare:

class Foo
{
}; ?

I thought 4. As in:

class Foo
{
public:
Foo(); // default constructor
Foo(const Foo& f); // default copy constructor

~Foo(); // default destructor
Foo &operator(const Foo& f); // default assigment operator
};

Is this list correct? Is that all?

Someone said you must also add the new and delete operators, so that's
6. I have not read the standard, but I'd argue that if this is so, then
you should also count operator* () and operator& ()

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> In a recent discussion, some of us were in disagreement about the
> functions the C++ compiler generates. How many functions are generated
> by the compiler when you declare:
>
> class Foo
> {
> }; ?
>
> I thought 4. As in:
>
> class Foo
> {
> public:
> Foo(); // default constructor
> Foo(const Foo& f); // default copy constructor


This is called "a copy constructor", not "default copy constructor"

> ~Foo(); // default destructor


There is no such thing as a "default destructor". There is always
only one destructor.

> Foo &operator(const Foo& f); // default assigment operator


Probably just a typo. Should be:

Foo &operator=(const Foo& f);


And this is actually called "copy assignment operator".

> };
>
> Is this list correct? Is that all?


Yes. Yes.

> Someone said you must also add the new and delete operators, so that's
> 6. I have not read the standard, but I'd argue that if this is so,
> then you should also count operator* () and operator& ()


There is no "default" operator* for a class. If the class doesn't
define its own 'new' and 'delete', the global ones are used. Also,
there is no action you can take to make compiler omit those, so we
usually don't count them. You *can* do something to make compiler not
to generate a default c-tor, a copy-c-tor, or a copy-assignment-op.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-25-2006
(E-Mail Removed) wrote:
> ...
> In a recent discussion, some of us were in disagreement about the
> functions the C++ compiler generates. How many functions are generated
> by the compiler when you declare:
>
> class Foo
> {
> }; ?
>
> I thought 4. As in:
>
> class Foo
> {
> public:
> Foo(); // default constructor
> Foo(const Foo& f); // default copy constructor
>
> ~Foo(); // default destructor
> Foo &operator(const Foo& f); // default assigment operator
> };
>
> Is this list correct? Is that all?


If by "compiler-generated" you mean "implicitly _declared_ by the compiler",
then you are right - that is all. As was noted before, the usage of the term
"default" in the comments is incorrect. The first one is indeed the default
constructor, while the rest are not "default" in any way.

The compiler will not _define_ these functions until you actually make an
attempt to use them in your code.

> Someone said you must also add the new and delete operators, so that's
> 6. I have not read the standard, but I'd argue that if this is so, then
> you should also count operator* () and operator& ()


No. Nothing else should be there.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Uday Bidkar
Guest
Posts: n/a
 
      07-26-2006

(E-Mail Removed) wrote:

> Hi,
>
> In a recent discussion, some of us were in disagreement about the
> functions the C++ compiler generates. How many functions are generated
> by the compiler when you declare:
>
> class Foo
> {
> }; ?
>
> I thought 4. As in:
>
> class Foo
> {
> public:
> Foo(); // default constructor
> Foo(const Foo& f); // default copy constructor
>
> ~Foo(); // default destructor
> Foo &operator(const Foo& f); // default assigment operator
> };
>
> Is this list correct? Is that all?
>
> Someone said you must also add the new and delete operators, so that's
> 6. I have not read the standard, but I'd argue that if this is so, then
> you should also count operator* () and operator& ()


Hi,
Going bit further, if you have a class hierachy lets say
class Base
{
};
class Dervd : public Base
{
};
then compiler also provides a type conversion operator for class
Dervd to allow
conversion from Der1* to Base*

Regards,
Uday Bidkar

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-26-2006
Uday Bidkar wrote:
> [..]
> Going bit further, if you have a class hierachy lets say
> class Base
> {
> };
> class Dervd : public Base
> {
> };
> then compiler also provides a type conversion operator for class
> Dervd to allow
> conversion from Der1* to Base*


Yes, but *you* can't do that. That conversion is _standard_ and is
always going to be there if the conditions are met (base class has to
be accessible and non-ambiguous). Try providing a conversion from
a pointer to A to a pointer to B if A and B are unrelated...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
Passing member functions to C functions? Steven T. Hatton C++ 7 10-07-2004 02:12 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Help! Passing Templates functions to template functions ILLOGIC C++ 1 06-01-2004 10:51 PM
Exportable class functions as stand alone functions to .DLL or .SO Timothy Wong C++ 3 05-20-2004 01:44 PM



Advertisments