473,695 Members | 1,998 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(co nst 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 9491
mo********@yaho o.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(co nst Foo& f); // default assigment operator
Probably just a typo. Should be:

Foo &operator=(cons t 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********@yaho o.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(co nst 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********@yaho o.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(co nst 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
2331
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 Python from source, because it also requires a change to Grammar. 1. Edit Python-2.3/Grammar/Grammar and add an alternative to the "listmaker" production: -listmaker: test ( list_for | (',' test)* )
10
2557
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 forgets to implement some method I normally get a compile error(if the class is created in some way of course). When using the Visual Studio 6 compiler however, it does not generate a compile error in this case: - I have a implementing class A...
7
3105
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 result. Especially, I want to know the compiler conformance of vc7.1, vc8.0, gcc 4.x and intel c++, and also each implementation of STL. The world is moving on, right? :)
0
2400
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 this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are closer to the point at which the
3
5263
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 (0xc0000005 at address 535F072A): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are...
4
3317
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 property 'DocItem' should be 'DocItems.' The problem is that instead of notifying me of where the problem has occurred, the compiler just crashes with an "internal error" (see bottom of this message). Now if I were to write: ...
1
1900
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 Amd64 cross-compiler from DDK 3790 build (proven to be good enough for my driver stuff) over the sources I've updated shortly I've found this serious assertion appearing in manyplaces of the code we have: $ tcc.ver edzunet build5105
0
1541
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", "credits" or "license" for more information. Module(None, Stmt(, Const('red')), Assign(, Const('blue'))])) <compiler.visitor.ExampleASTVisitor instance at 0x81f0eac>
6
2946
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++ standard. But mainly looking for compilers for small platforms like ARM, XScale, OMAP processors, and mobile OS's. I am not getting enough imformation which of the platforms HAVE a C++ compiler/cross compiler and how much they support C++ standard. It...
41
18181
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
0
9115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8845
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7660
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6491
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.