C ++ - 基类指针,方法指针,引用类,方法? [英] C++ - Base class pointer, method pointer, refer to derived class, method?

查看:170
本文介绍了C ++ - 基类指针,方法指针,引用类,方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Base 
{
virtual void Foo(){}
virtual void Bar(){}
};

class Derived1:public Base
{
void Foo(){// Do something}
void Bar(){// Do something}
}

class Derived2:public Base
{
void Foo(){// Do something}
void Bar(){// Do something} b}

class OtherClass
{
public:
Base * obj;
void(Base :: * method)();

void添加(Base * _obj,void(Base :: * _ method)())
{
obj = _obj;
method = _method;
}

void Run()
{
(obj-> method)();
}
}

int main()
{
Derived1 d1;
Derived2 d2;

OtherClass其他;

other.Add(& d1,&(Derived1 :: Foo));
//other.Add(& d2,&(Derived2 :: Bar));等等。

other.Run();
}



我的问题:



说我有一个方法的派生类,我可以引用一个类的实例,它的基类型的指针。假设我知道我想调用什么方法,那么我可以通过该指针调用它,并且将调用派生类的方法。



如何实现类似的多态行为我通过提供一个方法指针来指定要调用的方法?



上面的真正的代码基于将编译如果我强制转换方法指针,但似乎不是正在做任何 - 这是我已经意识到我没有调用OtherClass的更新方法,这就是为什么我没有得到任何喜悦。 :D所以这个工作,就像(几乎)。愚蠢的,愚蠢的大脑。



稍微修改课程:现在我需要静态转换方法指针传递给OtherClass,当我传递给Base类方法的指针它添加。这是不理想的。



例如,如果我传递&(Base :: Foo)到方法Add?

如果我在一个指向派生类型的实例的基类型的指针上调用这个方法,会调用Derived1 :: Foo?



我得到的感觉就是基本成员。 :(



有些阅读:
是否可以安全地上传方法指针并将其与基类指针一起使用?
< a href =http://stackoverflow.com/questions/60000/c-inheritance-and-member-function-pointers> C ++继承和成员函数指针
指向成员转换的指​​针
将指向派生类的方法的指针转换为指向基类的方法

解决方案

我相信你在想一个虚拟的member-fn-ptr如果在派生类中提供,base将触发多态派生重写。如果是,答案是肯定的,下面的代码演示了这一点。



希望这有帮助。

  #include< iostream> 

class Base
{
public:
virtual void Foo()
{
std :: cout< __PRETTY_FUNCTION__<< '\\\
';
}
virtual void Bar()
{
std :: cout< __PRETTY_FUNCTION__<< '\\\
';
}
};

class Derived1:public Base
{
public:
void Foo()
{
std :: cout< __PRETTY_FUNCTION__<< '\\\
';
}

void Bar()
{
std :: cout< __PRETTY_FUNCTION__<< '\\\
';
}
};

class Derived2:public Base
{
public:
void Foo()
{
std :: cout< __PRETTY_FUNCTION__<< '\\\
';
}

void Bar()
{
std :: cout< __PRETTY_FUNCTION__<< '\\\
';
}
};

class OtherClass
{
public:
Base * obj;
void(Base :: * method)();

void添加(Base * _obj,void(Base :: * _ method)())
{
obj = _obj;
method = _method;
}

void Run()
{
(obj-> * method)();
}
};

int main()
{
Derived1 d1;
Derived2 d2;

OtherClass其他;

other.Add(& d1,& Base :: Foo);
other.Run();

other.Add(& d2,& Base :: Bar);
other.Run();
}

输出

  virtual void Derived1 :: Foo()
virtual void Derived2 :: Bar()


class Base
{
    virtual void Foo(){}
    virtual void Bar(){}
};

class Derived1 : public Base
{
    void Foo(){ //Do something }
    void Bar(){ //Do something }
}

class Derived2 : public Base
{
    void Foo(){ //Do something }
    void Bar(){ //Do something }
}

class OtherClass
{
public:
    Base* obj;
    void (Base::*method)();

    void Add( Base* _obj, void (Base::*_method)() )
    {
        obj = _obj;
        method = _method;
    }

    void Run()
    {
        ( obj->method )();
    }
}

int main()
{
    Derived1 d1;
    Derived2 d2;

    OtherClass other;

    other.Add( &d1, &(Derived1::Foo) );
    //other.Add( &d2, &(Derived2::Bar) ); etc, etc.

    other.Run();
}

My question:

Say I have a derived class with a method, I can refer to an instance of that class with a pointer of it's base type. Assuming I know what method I want to call, I can then call it via that pointer and the derived class's method will be called.

How can I achieve similar polymorphic behaviour when I specify the method to be called by supplying a method pointer?

The real code the above is based on will compile if I cast the method pointer, but it appears to not be doing any-- It is at this point I've realised I wasn't calling OtherClass's update method, which is why I wasn't getting any joy. :D So this works, as is (almost). Stupid, stupid brain.

Slight course correction then: Right now I need to static cast the method pointer I pass to OtherClass, to a pointer to Base class method when I pass it to Add. This is not ideal.

Can I get the same behaviour if I pass &(Base::Foo), for example, to the method Add?

Will Derived1::Foo be called if I invoke that method on a pointer to base type that refers to an instance of the derived type?

I get the feeling it'll call the base member. :(

Some reading: Is it safe to "upcast" a method pointer and use it with base class pointer? C++ inheritance and member function pointers Pointer to member conversion Casting a pointer to a method of a derived class to a pointer to a method of a base class

解决方案

i believe you're pondering whether a member-fn-ptr of a virtual base will fire the polymorphic derivation override if provided in a derived class. If so, the answer is yes, and the code below demonstrate this.

Hope this helps.

#include <iostream>

class Base
{
public:
    virtual void Foo()
    {
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }
    virtual void Bar()
    {
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }
};

class Derived1 : public Base
{
public:
    void Foo()
    {
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }

    void Bar()
    {
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }
};

class Derived2 : public Base
{
public:
    void Foo()
    {
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }

    void Bar()
    {
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }
};

class OtherClass
{
public:
    Base* obj;
    void (Base::*method)();

    void Add( Base* _obj, void (Base::*_method)() )
    {
        obj = _obj;
        method = _method;
    }

    void Run()
    {
        (obj->*method)();
    }
};

int main()
{
    Derived1 d1;
    Derived2 d2;

    OtherClass other;

    other.Add( &d1, &Base::Foo );
    other.Run();

    other.Add( &d2, &Base::Bar);
    other.Run();
}

Output

virtual void Derived1::Foo()
virtual void Derived2::Bar()

这篇关于C ++ - 基类指针,方法指针,引用类,方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆