Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Derived::Derived(const Base&) and Derived& operator=(const Base&)

Reply
Thread Tools

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

 
 
developereo@hotmail.com
Guest
Posts: n/a
 
      05-23-2007
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-23-2007
wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57