473,499 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiler-Generated Default Functions

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

Jul 25 '06 #1
4 9460
mo********@yahoo.com 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
Jul 25 '06 #2
mo********@yahoo.com 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
Jul 25 '06 #3

mo********@yahoo.com 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

Jul 26 '06 #4
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
Jul 26 '06 #5

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

Similar topics

2
2307
by: Jeff Epler | last post by:
Hello. Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild...
10
2531
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
7
3089
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I saw cuj's conformance roundup, but the result is quite old. I think many people like me want to know newer c++ standard conformance test...
0
2385
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around...
3
5240
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error...
4
3293
by: David Sworder | last post by:
Consider the following line of code (it's not important what it does): resp.DocItem=Relations.SelectDocItems_BySearchString(req.SearchPhrase); It turns out that this line is in error. The...
1
1879
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
0
1532
by: skip | last post by:
Here's a trivial little Python session which attempts to use compiler.walk (mostly unsuccessfully): % python Python 2.4.2 (#1, Feb 23 2006, 12:48:31) on sunos5 Type "help", "copyright",...
6
2924
by: toton | last post by:
Hi, Anyone have a link to comparative study of different C++ compilers and how much they conform to C++ language standard? Most of the big platforms I know have GCC which well supports C++...
41
18053
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.