 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
walkman Guest
|
Posted: Thu May 03, 2007 2:45 am Post subject: Partial overwriting of a method in subclasses |
|
|
Hello,
I just bumped into a behaviour of C++ compilers which I do not
understand entirely and I would be glad is somebody could explain me
closer why compilers behave as they do and possibly point me to a
source (in e.g. C++ standard) where I can find more info.
Consider the following code:
// data classes
class A {};
class B : public A {};
// service classes
class C
{
public:
virtual ~C() {};
virtual void func(A& guest) {}
virtual void func(B& guest) {}
};
class D : public C
{
public:
virtual ~D() {};
virtual void visit(A& guest) { func(guest); }
virtual void func(B& guest) {}
};
Basically classes C and D implement a kind of visitor of the A, B
class hierarchy where in D, only the version of func(A&) is
overwritten and func(A&) is intended to stay the same as in the parent
class (i.e. C).
Now, this code does not compile in GCC 4.1.2, nor does it compile in
the online Comeau C++ form (http://www.comeaucomputing.com/tryitout/),
where the first complains about a lack of definition of D::func(A&)
and the later about imposibility to cast down A& to B& so that it
could use D::func(B&) in D::visit(...), which is finally the same
problem.
When I do not try to overwrite neither version of func(...),
everything is alright. Also when I overwrite both of them everything
is alright. Moreover Comeau C++ issues a warning that I only
"partially overwrote" the method func(...). I also noticed that the
fact that B is a subclass of A has nothing to do with the problem.
Questions:
1.- am I always forced to overwrite all versions of a method with the
same name, but with different signature?
2.- why cannot compiler figure out itself that in D::visit(A&) the
call to C::func(A&) is the right solution? It seems to me natural as
this is how I always thought this kind of polymorphism works
(obviously wrong!)... Do I really have to explicitly implement it this
way?:
virtual void visit(A& guest) {C::func(guest)} - "C::" seems
superfluous...
3.- does this something to do with the fact that C++ implements only
single-dispatch, instead of double-dispatch? (yet, when func is not
overwritten in D at all, compiler figure stuff out correctly!)
4.- how can I solve this problem without overwriting all the versions
of func(...)? In my real application C and D are visitors of an
extensive hierarchy and overloading all the versions of func(...)
would result into a big code bloat and explicit call to C::func() as
described in point 2 seems ugly to me.
Thanks for any explanations and clarifications.
Peter.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jerry Coffin Guest
|
Posted: Fri May 04, 2007 8:24 am Post subject: Re: Partial overwriting of a method in subclasses |
|
|
In article <f1c5eg$61q$1 (AT) news (DOT) dtag.de>, DerTopper (AT) web (DOT) de says...
[ ... ]
| Quote: | You have fallen into a trap that C++ lays out for everyone who start
object-oriented programming. What you see is the following behaviour:
If you overwrite a method in a derived class, all methods with the same
_name_ (not signature!) from the base class are invisible in the derived class.
|
I think it's only fair to point out that while this is a bit of a trap
for beginners, the alternative is a trap from which even the most
experienced couldn't escape...
--
Later,
Jerry.
The universe is a figment of its own imagination.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|