Compiler-Generated Default Functions

M

moleskyca1

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& ()
 
V

Victor Bazarov

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
 
A

Andrey Tarasevich

...
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.
 
U

Uday Bidkar

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
 
V

Victor Bazarov

Uday said:
[..]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top