473,725 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoid automatic copy constructor generation

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
different functionality. But is makes sense to do assignments and copy
construction between these set of types. This effectively changes the
functionality depending on the current type.
class ContainerBase
{ // ...
};

class Container1
: public ContainerBase
{ // no own data members!
public:
Container1()
{}
Container1(cons t ContainerBase& r)
: ContainerBase(r )
{}
Container1& operator=(const ContainerBase& r)
{ ContainerBase:: operator=(r); return *this; }
};

class Container2
: public ContainerBase
{ // no own data members!
public:
Container2()
{}
Container2(cons t ContainerBase& r)
: ContainerBase(r )
{}
Container2& operator=(const ContainerBase& r)
{ ContainerBase:: operator=(r); return *this; }
};
int main()
{ Container1 c1;
Container2 c2 = c1; // OK
Container1 c3 = c1; // Wrong! Invokes Container1(cons t Contarine1& r)
return 0;
}
Of course, I could implement the copy constructors to do the same thing
as the constructor from const ContainerBase&. But this is really
unneeded redundancy. And the copy constructor is much more complicated
in the real application than in this small example. Furthermore the auto
generated copy constructor is unusable. It has undefined behavior
because of some special internal storage dependencies. It also has to do
with multiple inheritance, so I could not move the code to a member
function either.

Is there a way to avoid the required redefinition of the copy
constructor for each class?
Marcel
Oct 25 '08 #1
34 3673
Marcel Müller wrote:
is there a way to avoid the automatic copy constructor generation.
Yes: Implement your own copy constructor. That will make the compiler
to not to create a default one.
Oct 25 '08 #2
Marcel Müller wrote:
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
different functionality. But is makes sense to do assignments and copy
construction between these set of types. This effectively changes the
functionality depending on the current type.
[snip]

What about implementing a copy-constructor for the Base and then have

class Derived_1 : public Base {

Derived_1 ( Base const & other )
: Base ( other )
{}

Derived_1 ( Derived_1 const & other )
: Base ( other )
{}

};

and similarly for Derived_2, ... ?

I think, if the copy-constructor for Base is implemented properly, the
compiler-generated copy-constructor for the derived classes would do the
same thing as the above, so you could even ditch that. Now, _that_ looks
very much like the code, I snipped. I clearly do not understand your
problem. Maybe, you need to post more details, because I lack the
imagination for:
Furthermore the auto
generated copy constructor is unusable. It has undefined behavior
because of some special internal storage dependencies. It also has to do
with multiple inheritance, so I could not move the code to a member
function either.
If that is true, I do not see how the constructors _from_ the base can
possibly work correctly if they are just:
Container1(cons t ContainerBase& r)
: ContainerBase(r )
{}


Best

Kai-Uwe Bux
Oct 25 '08 #3
Juha Nieminen wrote:
Marcel Müller wrote:
>is there a way to avoid the automatic copy constructor generation.

Yes: Implement your own copy constructor. That will make the compiler
to not to create a default one.
You don't have to implement it. Just declare one. If your program does
not use it, you don't have to define it. And to make sure the program
does not use it, declare it 'private'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 26 '08 #4
On 2008-10-25 21:55:54 -0400, Victor Bazarov <v.********@com Acast.netsaid:
Juha Nieminen wrote:
>Marcel Müller wrote:
>>is there a way to avoid the automatic copy constructor generation.

Yes: Implement your own copy constructor. That will make the compiler
to not to create a default one.

You don't have to implement it. Just declare one. If your program
does not use it, you don't have to define it. And to make sure the
program does not use it, declare it 'private'.
And a note for the future: with C++0X you don't need to declare it private.

class C
{
public:
C(const C&) = delete;
};

The "= delete" says that it doesn't exist. There's also "= default", to
tell the compiler to generate the default version even if it wouldn't
otherwise. These also apply to default constructors, generated
assignment operators, etc.

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

Oct 26 '08 #5
Hi,

Pete Becker schrieb:
And a note for the future: with C++0X you don't need to declare it private.

class C
{
public:
C(const C&) = delete;
};
thanks! This is the solution.

Unfortunately all my compilers are far away from C++0X, so I have to do
it the hard way so far.
Marcel
Oct 26 '08 #6
"Pete Becker" wrote:
>You don't have to implement it. Just declare one. If your program does
not use it, you don't have to define it. And to make sure the program
does not use it, declare it 'private'.

And a note for the future: with C++0X you don't need to declare it
private.

class C
{
public:
C(const C&) = delete;
};

The "= delete" says that it doesn't exist. There's also "= default", to
tell the compiler to generate the default version even if it wouldn't
otherwise. These also apply to default constructors, generated assignment
operators, etc.
I might like that. That's the first exposure I have had to this new
language of which you speak. I think if it is going to take off, it needs a
better name. I thought, and still do, that C++ is a bad name. The God
awful moniker of this thing is even worse than C++ is.

It kind of reminds me of the Microsoft's annoying obsession to use
meaningless words such as "Explorer". I think the idea was that "Explorer"
is a better "Navigator" . Maybe people would think that "Gamma" was a better
"C"?
Oct 26 '08 #7
On 2008-10-26 10:53:05 -0400, "osmium" <r1********@com cast.netsaid:
>
I might like that. That's the first exposure I have had to this new
language of which you speak. I think if it is going to take off, it needs a
better name. I thought, and still do, that C++ is a bad name. The God
awful moniker of this thing is even worse than C++ is.
The name C++0x is used to refer to the upcoming standard. Once it
becomes a standard, the language it defines will be called C++.

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

Oct 26 '08 #8
Pete Becker wrote:
On 2008-10-25 21:55:54 -0400, Victor Bazarov <v.********@com Acast.net>
said:
>Juha Nieminen wrote:
>>Marcel Müller wrote:
is there a way to avoid the automatic copy constructor generation.

Yes: Implement your own copy constructor. That will make the compiler
to not to create a default one.

You don't have to implement it. Just declare one. If your program
does not use it, you don't have to define it. And to make sure the
program does not use it, declare it 'private'.

And a note for the future: with C++0X you don't need to declare it private.

class C
{
public:
C(const C&) = delete;
};

The "= delete" says that it doesn't exist. There's also "= default", to
tell the compiler to generate the default version even if it wouldn't
otherwise. These also apply to default constructors, generated
assignment operators, etc.
I can see the benefit of "= default", but what benefits does "= delete"
offer over private constructors?

--
Ian Collins
Oct 26 '08 #9
On 2008-10-26 17:37:53 -0400, Ian Collins <ia******@hotma il.comsaid:
Pete Becker wrote:
>On 2008-10-25 21:55:54 -0400, Victor Bazarov <v.********@com Acast.net>
said:
>>Juha Nieminen wrote:
Marcel Müller wrote:
is there a way to avoid the automatic copy constructor generation.

Yes: Implement your own copy constructor. That will make the compiler
to not to create a default one.

You don't have to implement it. Just declare one. If your program
does not use it, you don't have to define it. And to make sure the
program does not use it, declare it 'private'.

And a note for the future: with C++0X you don't need to declare it private.

class C
{
public:
C(const C&) = delete;
};

The "= delete" says that it doesn't exist. There's also "= default", to
tell the compiler to generate the default version even if it wouldn't
otherwise. These also apply to default constructors, generated
assignment operators, etc.
I can see the benefit of "= default", but what benefits does "= delete"
offer over private constructors?
A bit of clarity. A private constructor can be implemented, and used by
members and friends. "= delete" says it doesn't exist.

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

Oct 27 '08 #10

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

Similar topics

9
2897
by: puzzlecracker | last post by:
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. };
0
8888
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
8752
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
9401
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
9176
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
6011
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
4519
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.