Derived::Derived(const Base&) and Derived& operator=(const Base&)

D

developereo

Hi folks,

Can somebody shed some light on this problem?

class Interface {
protected:
Interface() { ...}
virtual ~Interface() { ... }

public:
virtual method() = 0;
}

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

The problem is that the compiler (VC 2005) insists on generating
Impl1(const Impl1&) // copy constructor
and
Impl& operator=(const Impl1&) // default assignment operator

The problem is I do not want these methods.
I want Impl1 to be able to construct/assign itself from any (other)
Impl
satisfying the Interface. That's the whole point of having an
Interface.

Why can't the compiler use my supplied methods, since every Impl1 can
be implicitly cast down to an Interface anyway?

Thanks,
J.
 
V

Victor Bazarov

Hi folks,

Can somebody shed some light on this problem?

class Interface {
protected:
Interface() { ...}
virtual ~Interface() { ... }

public:
virtual method() = 0;
}

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

The problem is that the compiler (VC 2005) insists on generating
Impl1(const Impl1&) // copy constructor
and
Impl& operator=(const Impl1&) // default assignment operator

The problem is I do not want these methods.

Instead of fighting the compiler (and the language), it's better
to work around the limitations they impose. Define those things
and make them do the same thing as the other two. You can even
redirect the assignment op; you cannot redirect the constructor.
Yet, anyway.
I want Impl1 to be able to construct/assign itself from any (other)
Impl
satisfying the Interface. That's the whole point of having an
Interface.

The problem is that Impl1 assigned (or constructed) from another
Impl1 will still use the *proper* copy-assignment op and copy-c-tor.
Why can't the compiler use my supplied methods, since every Impl1 can
be implicitly cast down to an Interface anyway?

Because no conversions is better than a single conversion.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top