C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Compiler Generated Default Functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Wed Jul 26, 2006 1:50 am    Post subject: Compiler Generated Default Functions Reply with quote



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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Allan W
Guest





PostPosted: Thu Jul 27, 2006 12:36 am    Post subject: Re: Compiler Generated Default Functions Reply with quote



kanze wrote:
Quote:
The operator& is the hard one. It's defined for all objects, of
all types. The standard doesn't consider it an implicitly
defined operator; it says that the built in operator is used if
there is no user defined one. And in fact, there is no way to
define the operator to give the semantics of the built-in
operator.

How about...

class Foo {
// ...
public:
Foo* operator&() { return this; }
const Foo* operator&() const { return this; }
// ...
};

Quote:
Technically, when the compiler implicitly generates a function,
it behaves like a user defined function; the compiler generated
operator=, for example, has an address, participates in overload
resolution, and introduces sequence points. The global
operator& which the compiler uses if their is not a user defined
one requires an lvalue (which a user defined function cannot),
doesn't have an address, and doesn't introduce sequence points.

So a user-defined operator& can't be completely identical to the
built-in operator&. But "no way... to give the [same] semantics" is
still a bit overstated, isn't it?

I have found exactly one legal way to tell if operator& is user-defined
or not -- take the address.

Foo* (Foo::*p)() = &(Foo::operator &);

This fails if operator& is not user-defined.

Is there any other difference, in legal code?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matthias Hofmann
Guest





PostPosted: Thu Jul 27, 2006 7:58 pm    Post subject: Re: Compiler Generated Default Functions Reply with quote



"kanze" <kanze@gabi-soft.fr> schrieb im Newsbeitrag
news:1153919089.902645.33960 (AT) i42g2000cwa (DOT) googlegroups.com...

Quote:
extern void* operator new( size_t, int ) ;

struct A {} ;

A* p = new A ; // Not an error, although a compiler
// generated default would hide the
// global operator.


If I understand this correctly, the allocation of a new A should invoke the
above declared placement version of operator new(), shouldn't it? But
doesn't this require the following?

// Pass 42 as the second
// argument of operator new().
A* p = new( 42 ) A;

Otherwise, I do not understand why the example you provided should not be an
error.

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.