* ectoplasm:
The problem is, that MS VC++ gives a compile error
error C2664: cannot convert parameter 1 from 'A *' to 'B *'
on my call DoSomething(pA) . Even though pA is a pointer to a B
instance.
That is not the problem, it's the compiler telling you you've chosen a bad
way to solve the problem.
Seems there is no way around this. A method can only take a parameter
of its type or a derived type. In other words, can only be an
(implicit) upcast. In my opinion, this is a failure of C++. Don't you
agree that what I want is not strange at all but is OO functionality
that naturally should be possible?
What you seem to want is not OO, it's the opposite.
However, my earlier comment stands: make 'DoSomething' a virtual member
function of class A. You then wrote, "the [two] methods I want to implement
shall not be (virtual) members of A", but that's OK. Here's one way --
and note that it's just a technical solution, it _does not_ solve the real
problem, which is at the design level, not the language level, and unknown:
#include <iostream>
#include <ostream>
class B;
class C;
static void doSomething( B* )
{
std::cout << "doSomethin g( B* )" << std::endl;
}
static void doSomething( C* )
{
std::cout << "doSomethin g( C* )" << std::endl;
}
class A
{
private:
virtual void doSomething() = 0;
public:
static void doSomething( A* arg ) { arg->doSomething( ); }
};
class B: public A
{
private:
virtual void DoSomething()
{
::doSomething( this );
}
};
class C: public A
{
private:
virtual void doSomething()
{
::doSomething( this );
}
};
void doSomething( A* arg )
{
A::doSomething( arg );
}
int main()
{
A* p = new C;
doSomething( p );
delete p;
}
--
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?