Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Member function pointers

0 views
Skip to first unread message

Christian Meier

unread,
Jun 4, 2009, 10:57:02 AM6/4/09
to
Hello group

I have a class with some functions which all have no parameters. They return
pointers to objects of different types but all of them have the same base
class.
Can I make a member function pointer in a way that I am able to assign any
of these functions although they have a different return type?

class Base
{
};

class Derived1 : public Base
{
Derived1* getterFunc() { return this; }
};

class Derived2 : public Base
{
Derived2* getterFunc() { return this; }
};


class Foo
{
public:
Derived1* getDerived1() { return derived1_; }
Derived2* getDerived2() { return derived2_; }
private:
Derived1* derived1_;
Derived2* derived2_;
};

int main()
{
typedef Base* (Foo::*FunctionPointer)();
// Does not compile.
//FunctionPointer bar = &Foo::getDerived1; <---------
}

I hoped that the compiler can convert the type "Derived1* (Foo::*)()" to
"Base* (Foo::*)()".
Is the only solution to change the return type of Foo::getDerived1() and
Foo::getDerived2() to "Base*"?
And can someone explain me, why such a conversion is not possible in c++?

Thanks for your answers!

Greets Chris


Victor Bazarov

unread,
Jun 4, 2009, 11:08:55 AM6/4/09
to
Christian Meier wrote:
> I have a class with some functions which all have no parameters. They return
> pointers to objects of different types but all of them have the same base
> class.
> Can I make a member function pointer in a way that I am able to assign any
> of these functions although they have a different return type?
>
> class Base
> {
> };
>
> class Derived1 : public Base
> {
> Derived1* getterFunc() { return this; }
> };
>
> class Derived2 : public Base
> {
> Derived2* getterFunc() { return this; }
> };

So, the hierarchy is not polymorphic...

>
>
> class Foo
> {
> public:
> Derived1* getDerived1() { return derived1_; }
> Derived2* getDerived2() { return derived2_; }
> private:
> Derived1* derived1_;
> Derived2* derived2_;
> };
>
> int main()
> {
> typedef Base* (Foo::*FunctionPointer)();
> // Does not compile.
> //FunctionPointer bar = &Foo::getDerived1; <---------
> }
>
> I hoped that the compiler can convert the type "Derived1* (Foo::*)()" to
> "Base* (Foo::*)()".

It can't. Those are two different types, no standard conversion exists
between them.

> Is the only solution to change the return type of Foo::getDerived1() and
> Foo::getDerived2() to "Base*"?

I am not sure it's of any value to you, though. Once you get the object
of type 'Base*', how do you know what type to 'static_cast' to? You
can't use 'dynamic_cast' since your hierarchy is not polymorphic. You
don't seem to have any way to determine what type the "parent" object has.

> And can someone explain me, why such a conversion is not possible in c++?

It's not possible because nobody ever needed it, so the language has
been defined not to have that conversion.

Generally speaking, it's not impossible to introduce such a conversion
into the language, but why bother? Your "problem" has another, simpler
solution. Your hierarchy probably ought to be polymorphic, and then you
just return 'Base*' and call virtual functions on those, or use
'dynamic_cast'...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Christian Meier

unread,
Jun 5, 2009, 2:41:32 AM6/5/09
to
Victor Bazarov wrote:

> Christian Meier wrote:
>> class Base
>> {
>> };
>>
>> class Derived1 : public Base
>> {
>> Derived1* getterFunc() { return this; }
>> };
>>
>> class Derived2 : public Base
>> {
>> Derived2* getterFunc() { return this; }
>> };
>
> So, the hierarchy is not polymorphic...

Ok, I see, that's an important detail I forgot in my sample code.
In our project the base class has some pure virtual functions.


> It's not possible because nobody ever needed it, so the language has been
> defined not to have that conversion.
>
> Generally speaking, it's not impossible to introduce such a conversion
> into the language, but why bother? Your "problem" has another, simpler
> solution. Your hierarchy probably ought to be polymorphic, and then you
> just return 'Base*' and call virtual functions on those, or use
> 'dynamic_cast'...

As I mentioned, I know that my problem would be solved if I changed the
return type to "Base*". And if I were able to change that code so easily
then I would do that.
I just hoped that there is a technical solution which works together with
the problem of not returning pointers to the base class.

Thanks for your answer. As usual, it was helpful!

Greets Chris


0 new messages