473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Derived::Derive d(const Base&)

Hi folks,

Can anybody shed some light on this problem?

class Interface {
public:
Interface() { ...}
virtual ~Interface() { ...}
virtual method() = 0;
};

class Impl1: public Interface {
public:
Impl1() { ...}
Impl1(const Interface&); // problem 1
virtual ~Impl1() { ... }
Impl1& operator=(const Interface&); // problem 2
};

The problem is that the compiler insists on generating the following
methods:
Impl1(const Impl1&); // copy constructor
Impl1& operator=(const Impl1&); // assignment operator
for me.
I do not need these methods.
I do not want these methods.
I would have thought the compiler would call one of my explicit
methods since every Impl1 is also an Interface.

Is there some simple trick I am missing here?

Thanks,
J.

May 22 '07 #1
4 2186
* de*********@hot mail.com:
Hi folks,

Can anybody shed some light on this problem?

class Interface {
public:
Interface() { ...}
virtual ~Interface() { ...}
virtual method() = 0;
};

class Impl1: public Interface {
public:
Impl1() { ...}
Impl1(const Interface&); // problem 1
virtual ~Impl1() { ... }
Impl1& operator=(const Interface&); // problem 2
};

The problem is that the compiler insists on generating the following
methods:
Impl1(const Impl1&); // copy constructor
Impl1& operator=(const Impl1&); // assignment operator
for me.
I do not need these methods.
I do not want these methods.
I would have thought the compiler would call one of my explicit
methods since every Impl1 is also an Interface.
No. Both the copy constructor and the copy assignment operator are very
special member functions (thus, listend under "Special member
functions"). They're generated if they're used and not declared.

Is there some simple trick I am missing here?
At the technical C++ level: just declare them.

But at the design level, having polymorphic assignment is almost never a
good idea.

Have you really thought through the consequences, how to handle all
combinations of destination and source (e.g., run time errors)?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 22 '07 #2

<de*********@ho tmail.comwrote
Hi folks,

Can anybody shed some light on this problem?

class Interface {
public:
Interface() { ...}
virtual ~Interface() { ...}
virtual method() = 0;
};

class Impl1: public Interface {
public:
Impl1() { ...}
Impl1(const Interface&); // problem 1
virtual ~Impl1() { ... }
Impl1& operator=(const Interface&); // problem 2
};

The problem is that the compiler insists on generating the following
methods:
Impl1(const Impl1&); // copy constructor
Impl1& operator=(const Impl1&); // assignment operator
for me.
I do not need these methods.
I do not want these methods.
I would have thought the compiler would call one of my explicit
methods since every Impl1 is also an Interface.

Is there some simple trick I am missing here?
The compiler needs the derived copy constructor for declaring and returning
derived objects.
The operator= however should follow the NVI (NonVirtual Interface) design
pattern explained in:
C++ Coding Standards, by Sutter and Alexandrescu,
That is operator= should be non-virtual and only be declared in the base
class, and simply call the virtual assign method, see item 55 in the book
mentioned.
As an application example of this and the NVI design pattern you may read
N2143 on
http://open-std.org/jtc1/sc22/wg21/d...mailing2007-01
and look at the integer and unsigned_intege r classes in the synopsis as an
example.
When making this I encountered the same problem as you did.
But integer and unsigned_intege r have the same non-static data members. When
the data members are different, you should be aware of the slicing problem,
discussed in item 54 of the book mentioned.
Hope this helps,
Maarten.
May 22 '07 #3
On May 22, 6:44 pm, "Alf P. Steinbach" <a...@start.now rote:
* develope...@hot mail.com:
Can anybody shed some light on this problem?
class Interface {
public:
Interface() { ...}
virtual ~Interface() { ...}
virtual method() = 0;
};
class Impl1: public Interface {
public:
Impl1() { ...}
Impl1(const Interface&); // problem 1
virtual ~Impl1() { ... }
Impl1& operator=(const Interface&); // problem 2
};
The problem is that the compiler insists on generating the following
methods:
Impl1(const Impl1&); // copy constructor
Impl1& operator=(const Impl1&); // assignment operator
for me.
I do not need these methods.
I do not want these methods.
I would have thought the compiler would call one of my explicit
methods since every Impl1 is also an Interface.
No. Both the copy constructor and the copy assignment operator are very
special member functions (thus, listend under "Special member
functions"). They're generated if they're used and not declared.
Is there some simple trick I am missing here?
At the technical C++ level: just declare them.
I don't think that will do what he wants. If I understand him
correctly, he wants Impl1( Interface const& ) to be used when
copying an Interface. In that case, the only solution he has is
to als define his Impl1( Impl1 const& ) to do exactly the same
thing.
But at the design level, having polymorphic assignment is almost never a
good idea.
Have you really thought through the consequences, how to handle all
combinations of destination and source (e.g., run time errors)?
Maybe he's implementing the letter/envelope idiom. (But
somehow, I don't think so, and I think you're right, copy and
assignment aren't going to work like he wants.)

--
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

May 23 '07 #4
* James Kanze:
On May 22, 6:44 pm, "Alf P. Steinbach" <a...@start.now rote:
>* develope...@hot mail.com:
>>Can anybody shed some light on this problem?
>>class Interface {
public:
Interface() { ...}
virtual ~Interface() { ...}
virtual method() = 0;
};
>>class Impl1: public Interface {
public:
Impl1() { ...}
Impl1(const Interface&); // problem 1
virtual ~Impl1() { ... }
Impl1& operator=(const Interface&); // problem 2
};
>>The problem is that the compiler insists on generating the following
methods:
Impl1(const Impl1&); // copy constructor
Impl1& operator=(const Impl1&); // assignment operator
for me.
I do not need these methods.
I do not want these methods.
I would have thought the compiler would call one of my explicit
methods since every Impl1 is also an Interface.
>No. Both the copy constructor and the copy assignment operator are very
special member functions (thus, listend under "Special member
functions"). They're generated if they're used and not declared.
>>Is there some simple trick I am missing here?
>At the technical C++ level: just declare them.

I don't think that will do what he wants. If I understand him
correctly, he wants Impl1( Interface const& ) to be used when
copying an Interface. In that case, the only solution he has is
to als define his Impl1( Impl1 const& ) to do exactly the same
thing.
First off, technicality: a definition is a declaration, so in a C++
technical interpretation that solution is included in what I said.

But just declaring them with no definition is, contrary to (the natural
and most sensible interpretation of) your statement, sufficient to
guarantee they'll not be invoked.

Instead of using static_cast it's then convenient to equip the Interface
class with an explicit asInterface() member function:

#include <iostream>
#include <ostream>

void say( char const s[] ) { std::cout << s << std::endl; }

class Interface
{
public:
Interface() {}
virtual ~Interface() {}
virtual void method() = 0;

virtual Interface& asInterface() { return *this; }
};

class Impl1: public Interface
{
private:
Impl1( Impl1 const& );
Impl1& operator=( Impl1 const& );

public:
Impl1() {}

Impl1( Interface const& )
{ say( "Copying interface" ); }

Impl1& operator=( const Interface& )
{ say( "= interface" ); return *this; }

void method() {}
};

int main()
{
Impl1 a;
Impl1 b( a.asInterface() );

a = b.asInterface() ;
}

>But at the design level, having polymorphic assignment is almost never a
good idea.
>Have you really thought through the consequences, how to handle all
combinations of destination and source (e.g., run time errors)?

Maybe he's implementing the letter/envelope idiom. (But
somehow, I don't think so, and I think you're right, copy and
assignment aren't going to work like he wants.)
Yes. Instead of copying to existing objects, he should probably be
considering cloning. And with a restriction to dynamic allocation the
asInterface function wouldn't be needed because all objects would be
handled via pointers or references to interfaces.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 23 '07 #5

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

Similar topics

3
8266
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it at all possible?
11
8104
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know how and why it works. I'll post a simplified version of it below and ask my questions afterwards: class Base { void *function_ptr;
2
2405
by: verec | last post by:
Consider a first version: --- drawable.hpp --- #include "gcdata.hpp" struct drawable { ... virtual int internal_new_GC(gcdata * gcd) = 0 ; } ; --- gcdata.hpp ---
10
40663
by: Robert | last post by:
Hello all, I am a C programmer learning C++. And I am confused with function Pass by Pointer * or Pass by Reference &. Function pass by pointer like: void func(int * x) And function pass by referencelike: void func(int & x)
10
2513
by: placid | last post by:
Hi all, I was just wondering, in Java there is an operator called instanceOf, whats the equivalent in c++? Thanks in advance to all.
12
3795
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. 1. The Deserialization goes like: #Region " Load "
6
2040
by: AzizMandar | last post by:
There is probably a better way to do this and if so I'm just as happy to see that way. I have a program where I have factories that each create various objects abstracted from a base class. The Factories all have a base class as well for the objects to point back to. So I have
3
2127
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think meets these requirements, but after reading the chapter on exceptions in 'Exceptional C++':Sutter, I am not sure if its is really Exception safe or Exception Neutral. I suppose putting the theory in that chapter into practice isn't trivial.
1
2128
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
0
9345
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
10115
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
9905
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
9775
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...
1
7332
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
5229
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
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
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
3
2752
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.