繁体   English   中英

就像虚函数一样,我们可以在C ++中使变量虚拟吗

[英]Like, virtual function can we make a variable virtual in c++

当基类指针指向其派生类的对象时,并且如果某个函数被重写,我们将使用虚拟函数来解决该问题。 这样我们就可以使用指针访问派生类的自身功能。 像这样,我在想,如果有一种方法可以应用于variable中的virtual关键字,以便我们可以使用指针访问派生类中变量的最新版本。

#include <iostream>
using namespace std;
class base
{
public:
int x;//but what about this , if we add virtual keyword here.
    //it will give error if trying to do so .
    //but can you tell me what can i do if i want to make use of it as virtual function
    //if not please tell me why
virtual void display(void) //to call the recent version of display function we make use of virtual here
{
    cout << "base\n";
}
};
class derived : public base
{
public:
int x;
void display(void)
{
    cout << "derived\n";
}
};
int main(void)
{
    base *p;
    base ob1;
    derived ob2;
    p=&ob2;
    p->x=100;//here i want to set 100 to the x of derived class not that x which has been inherited
            //But it sets to the x of base class which i dont wanted
    p->display();//here we can access the latest version of display function in derived class
    return 0;
}

拜托,没有人问我为什么要这样做。我没有任何打算在我的真实代码中这样做。 我要求好奇。

不,您不能将virtual用于字段,只能用于方法。

但是,您可以通过创建一个返回对字段的引用的函数来进行模拟:

class Base
{
private:
    int x;

public:
    virtual int& X() { return x; }
};

class Derived : public Base
{
private:
    int x;

public:
    virtual int& X() override { return x; }
};

int main()
{
    Derived d;
    Base* b = &d;

    b->X() = 100; // will set d's x
}

您不能使用virtual关键字覆盖成员变量。 但是,您可以在基类和派生类中使用virtual getter和setter引用不同的成员变量,以实现类似的效果:

class base {
public:
    virtual int getX() {
        return x;
    }
    virtual void setX(int x) {
        this->x = x;
    }
private:
    int x;
}

class derived : public base {
public:
    int getX() {
        return x;
    }
    void setX(int x) {
        this->x = x;
    }
private:
    int x;
}

其他答案完全可以,但您也可以使用更简单的语法:

class base {
public:
    virtual operator int&() { return x; };
    virtual operator int() { return x; };
protected:
    int x;
};

如果您要在类中虚拟化单个变量。

第二个声明只是避免在只需要值时使用引用,而在分配引用时会自动为您选择。

您可以从base派生的类中随意重写这些运算符。

class derived : public base {
    public:
    operator int() override { return x * 5; };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM