在c ++中继承类的情况下强制使用较晚的方法解析 [英] Forcing late method resolution in case of class inheritance in c++

查看:110
本文介绍了在c ++中继承类的情况下强制使用较晚的方法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下类结构: -

  class foo {
public:
int fun ){
cout<< in foo< endl;
}
};

class bar_class1:public foo {
public:
int fun(){
cout< in bar_class1< endl;
}
};

class bar_class2:public foo {
public:
float fun(){
cout< in bar_class2< endl;
}
};

main(){
foo * foo_pointer = new bar_class1();
foo_pointer-> fun();
}

上述程序的输出是 in foo 。有一种方法,使用 foo * 类型的指针实际指向 bar_class1 bar_class2 ,我们可以调用派生类的 fun 函数而不是基类?我不能在基类 foo 中创建 fun 函数virtual,因为有一个返回类型冲突对于 bar_class2

foo >



你不能这样做。



如果这种多态性是可能的,当代码在实际类型为bar_class2的对象上调用 foo :: fun (期望一个int)从而得到浮点数?你想简单地抛弃类型安全吗?



如果你想要不同的返回类型,听起来像你想要的模板。但是你不能使用foo()的方式使用模板。静态多态性(模板)和运行时多态性(后期绑定)不能很好地混合。你需要重新设计你的oop结构。



如果你绝对讨厌类型安全,你可以 使用void指针。 但是对于Flying Spaghetti Monster的爱,不要在c ++中这样做。请在阅读以下代码之前闭上眼睛,以避免接触。

  #include< iostream> 

class foo {
public:
virtual void * fun()= 0;
virtual〜foo(){};
};

class bar_class1:public foo {
public:
void * fun(){
return& value;
}
private:
int value = 1;
};

class bar_class2:public foo {
public:
void * fun(){
return& value;
}
private:
float value = 1.1;
};

int main(){
foo * foo_pointer1 = new bar_class1();
foo * foo_pointer2 = new bar_class2();

//在c ++编译器中必须知道编译期间所有对象的类型
std :: cout< * reinterpret_cast< int *>(foo_pointer1-> fun())< '\\\
';
std :: cout<< * reinterpret_cast< float *>(foo_pointer2-> fun())<< '\\\
';
delete foo_pointer1;
delete foo_pointer2;
}


Consider the following class structure:-

    class foo {
    public:
      int fun () {
        cout << "in foo" << endl;
      }
    };

    class bar_class1:public foo {
    public:
      int fun () {
        cout << "in bar_class1" << endl;
      }
    };

    class bar_class2:public foo {
    public:
      float fun () {
        cout << "in bar_class2" << endl;
      }
    };

    main () {
      foo * foo_pointer = new bar_class1();
      foo_pointer->fun();
    }

The output of the above program is in foo. Is there a way, that using a pointer of type foo * which actually points to an object of type bar_class1 or bar_class2, we can call the fun function of the derived class instead of the base class? I am not able to make the fun function virtual in the base class foo since, then there is a return type conflict for function foo in the derived class bar_class2.

解决方案

Here's my comments as an answer.

You cannot do that.

If that kind of polymorphism were possible, wouldn't that break horribly when code calls foo::fun (expecting an int) on an object whose actual type is bar_class2 and thus gets a float? Do you want to simply throw away type safety?

If you want different return types, sounds like you want a template. But you cannot use templates quite in the way that you want to use foo(). Static polymorphism (templates) and run time polymorphism (late binding) don't mix well. You need to redesign your oop structure.

If you absolutely hate type safety, you can sort of do this with void pointers. But for the love of Flying Spaghetti Monster, don't ever do this in c++. Please close your eyes before reading the following code to avoid exposure.

#include <iostream>

class foo {
public:
    virtual void* fun() = 0;
    virtual ~foo(){};
};

class bar_class1: public foo {
public:
    void* fun() {
        return &value;
    }
private:
    int value = 1;
};

class bar_class2: public foo {
public:
    void* fun() {
        return &value;
    }
private:
    float value = 1.1;
};

int main() {
    foo* foo_pointer1 = new bar_class1();
    foo* foo_pointer2 = new bar_class2();

    // in c++ compiler must know the type of all objects during compilation
    std::cout << *reinterpret_cast<int*>(foo_pointer1->fun()) << '\n';
    std::cout << *reinterpret_cast<float*>(foo_pointer2->fun()) << '\n';
    delete foo_pointer1;
    delete foo_pointer2;
}

这篇关于在c ++中继承类的情况下强制使用较晚的方法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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