473,837 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator)

From my understanding, if you declare any sort of constructors,
(excluding copy ctor), the default will not be included by default. Is
this correct?

class Foo{
public:
Foo(int); // no Foo() is included, i believe.
};

Under which conditions a default copy ctor and default assignment
operator are not included?
Thanks.

Jun 27 '08 #1
9 2906
"puzzlecrac ker" <ir*********@gm ail.comwrote in message
From my understanding, if you declare any sort of constructors,
(excluding copy ctor), the default will not be included by default. Is
this correct?
No, even declaring/defining an explicit copy c'tor would restrict compiler
from generating a default constructor.
class Foo{
public:
Foo(int); // no Foo() is included, i believe.
No default constructor would be generated in this case. Default assignment
operator won't be affected.
};

--
http://techytalk.googlepages.com
Jun 27 '08 #2
No, even declaring/defining an explicit copy c'tor would restrict compiler
from generating a default constructor.
What about the reverse - Will declaring an explicit default ctor,
cause a class to generate a default copy constructor?
How is assignment affected? Say we generate an overload version of
assignment operator, will a default be included in the class?
Thanks
Jun 27 '08 #3


"puzzlecrac ker" <ir*********@gm ail.comwrote in message
>
>No, even declaring/defining an explicit copy c'tor would restrict
compiler
from generating a default constructor.

What about the reverse - Will declaring an explicit default ctor,
cause a class to generate a default copy constructor?
Yes. Just from compilation perspectives it should be fine (Try it without
defining it).
How is assignment affected? Say we generate an overload version of
assignment operator, will a default be included in the class?
Yes
--
http://techytalk.googlepages.com
Jun 27 '08 #4
puzzlecracker wrote:
>No, even declaring/defining an explicit copy c'tor would restrict compiler
from generating a default constructor.

What about the reverse - Will declaring an explicit default ctor,
By "explicit" here you probably mean "user-declared". The term
"explicit" has a certain specific meaning when applied to constructors
in C++, you know...
cause a class to generate a default copy constructor?
Again, a problem with terminology. There's no such thing as "default
copy constructor". There's the default constructor. And there's the copy
constructor. Both can be ether user-declared or compiler-declared.

I assume your question is about the compiler-declared copy constructor.
Providing a user-declared default constructor will not really "cause"
anything to happen to compiler-declared copy constructor, i.e. it will
not prevent the compiler from declaring the copy constructor in
situations when you don't declare it yourself.
How is assignment affected? Say we generate an overload version of
assignment operator, will a default be included in the class?
Yes. Regardless of what other assignment operators you might declare, if
you don't declare the copy assignment operator, the compiler will
declare one for you.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #5
On Apr 14, 6:16 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
From my understanding, if you declare any sort of
constructors, (excluding copy ctor), the default will not be
included by default. Is this correct?
No. Any user defined constructor, including a copy constructor,
inhibits automatic generation of the default constructor.
class Foo{
public:
Foo(int); // no Foo() is included, i believe.

};
Under which conditions a default copy ctor and default
assignment operator are not included?
A copy constructor is generated any time there is no user
declared copy construtor. A copy assignment operator is
generated any time there is no user declared copy assignment
operator. A copy constructor for a class X is a non-template
constructor having an X&, X const&, X volatile& or X const
volatile& as its first parameter, and having default arguments
for all other parameters, if any. A copy assignment operator is
a non-template assignment operator having an X, X&, X const&, X
volatile& or X const volatile& as parameter. Note that:

-- a member function template template is never considered a
copy constructor or copy assignment operator (and thus will
never prevent the compiler from generating one);

-- you can declare more than one copy constructor or copy
assignment operator; and

-- operator overload resolution is *always* used when deciding
what constructor to call, even when "copying", and operator
overload resolution can choose a constructor which is not a
copy constructor to copy.

Consider:

class C
{
public:
template< typename T >
C( T& other ) ;
} ;

The compiler will generate a C::C( C const& ) copy constructor,
which will participate in operator overload resolution:

C c1 ;
C c2( c1 ) ; // calls the template constructor.
C c3( C() ) ; // calls the compiler generated copy
// constructor.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
On 2008-04-15 05:18:40 -0400, James Kanze <ja*********@gm ail.comsaid:
On Apr 14, 6:16 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
>From my understanding, if you declare any sort of
constructors , (excluding copy ctor), the default will not be
included by default. Is this correct?

No. Any user defined constructor, including a copy constructor,
inhibits automatic generation of the default constructor.
Longer term, i.e. when C++0x is finalized, the solution to this problem
will be to tell the compiler to generate the default constructor
despite the existence of other constructors.

struct foo {
foo() = default;
foo(const foo&);
};

You'll also be able to tell the compiler not to generate a default constructor:

struct foo {
foo() = delete;
};

These modifiers will be applicable to all of the special member functions.

And before anyone asks: I don't know whether any compiler currently
implements this.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #7
>
class C
{
public:
template< typename T >
C( T& other ) ;
} ;
C c1 ;
C c2( c1 ) ; // calls the template constructor.
C c3( C() ) ; // calls the compiler generated copy
// constructor.
That's interesting, I didn't know template copy ctor that takes non-
const, supersedes non-template ctor that take const type, when user
passes const type
Jun 27 '08 #8
sasha wrote:
> class C
{
public:
template< typename T >
C( T& other ) ;
} ;
C c1 ;
C c2( c1 ) ; // calls the template constructor.
C c3( C() ) ; // calls the compiler generated copy
// constructor.

That's interesting, I didn't know template copy ctor that takes non-
const, supersedes non-template ctor that take const type, when user
passes const type
Sorry, but what you said doesn't seem to make much sense.

Firstly, there's no const type in this example.

Secondly, when user does actually use an initializer of const type, the
constructor that takes a non-const reference will not even qualify as a
viable function. So no, it will not supersede anything. The behavior
will be exactly opposite of what you said.

How did you manage to derive these strange conclusions from the previous
post from James is a mystery to me.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #9
On 15 avr, 19:10, sasha <aborovin...@gm ail.comwrote:
class C
{
public:
template< typename T >
C( T& other ) ;
} ;
C c1 ;
C c2( c1 ) ; // calls the template constructor.
C c3( C() ) ; // calls the compiler generated copy
// constructor.
That's interesting, I didn't know template copy ctor that
takes non- const, supersedes non-template ctor that take const
type, when user passes const type
I don't understand your reasoning. There's not a single const
object in my example. The template constructor is chosen when
the argument is a non-const lvalue, because the instantiation of
the template (C::C(C&)) is a better match. The compiler
generated copy constructor has the signature C::C( C const& )
(in this case), and will be called when the argument is const,
or isn't an lvalue---both case in which the template constructor
couldn't be called.

Note that the compiler generated copy constructor doesn't always
take a const reference---if the class contains a member or has a
base whose copy constructor (user provided) takes a non-const
reference, the the compiler generated copy constructor will also
take a non-const reference. Such cases are (hopefully) rare,
however.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10

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

Similar topics

3
1741
by: Bob | last post by:
Hi, If a class contains only built-in types and standard library objects such as strings and vectors, is it safe to let the compiler create the copy constructor and assignment operator, or is it advised to always write your own? I know you need to write your own if there is any dynamic memory allocation etc, but I was curious as to the "correct" way in the case of static variables.
1
1718
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template works for many different types. The one that I use is named Integer and is a Wrapper for an int....
9
5807
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct ValueStruct { double x ; double y ; ValueStruct() { x = y = double(0) ; } // error * } ;
5
2278
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor of "value" type of map, I get the following error: // -------------------------------------------- /usr/include/c++/3.2.3/bits/stl_map.h:225: no matching function for call to `FieldType::FieldType()'
43
3850
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
3
2060
by: subramanian100in | last post by:
If we provide any ctor for a class the compiler does not supply the default ctor. However if we do not provide the copy ctor but provide any other ctor, the compiler still supplies the copy ctor. Why doesn't the compiler supply the default ctor but still supplies the copy ctor when we have defined any other ctor ? Kindly explain
15
1839
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class Test { public: Test(int xx) : x(xx) { cout << x << endl; }
34
3701
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But there is a constructor that accepts the base class. This one should be used for copying. In fact i have a set of classes with a common abstract base. The implementations do not have own data members. They only implement
0
9837
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10267
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
9392
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
7804
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
6998
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();...
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.