为什么C ++没有虚拟变量? [英] Why doesn't C++ have virtual variables?

查看:109
本文介绍了为什么C ++没有虚拟变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一百万次被问到的问题,或者可能是非常愚蠢的,但是为什么没有实施?

  A类
{
public:
A(){a = 5;}
int a;
};

B类:公共A
{
public:
B(){a = 0.5;}
浮动a;
};

int main()
{
A * a =新的B();

cout< a-a;
getch();
返回0;
}

此代码将访问A :: a。如何访问B :: a?

解决方案

访问B :: a:

  cout<< static_cast< B *(a)-> a; 

显式访问A :: a和B :: a:

  cout<< static_cast< B *>(a)-> A :: a; 
cout<< static_cast< B *>(a)-> B :: a;

(dynamic_cast有时比static_cast更好,但是在这里不能使用,因为A和B是



为什么C ++没有虚拟变量:虚拟函数允许多态;换句话说,它们通过调用代码使两个不同类型的类被视为相同,而这两个类的内部行为的任何差异都封装在虚函数中。



虚拟成员变量实际上没有任何意义;



还请记住,C ++是静态类型的。虚函数使您可以在运行时更改行为。您的示例代码试图在运行时不仅更改行为,而且还更改数据类型 A :: a int B :: a float ),而C ++则无法正常工作。如果需要在运行时容纳不同的数据类型,则需要将这些差异封装在隐藏数据类型差异的虚拟函数中。例如(仅演示代码;对于真实代码,您超载运算符<<代替):

  A类
{
public:
A(){a = 5;}
int a;
virtual void output_to(ostream& o)const {o<<一种; }
};

B类:公共A
{
public:
B(){a = 0.5;}
浮动a;
void output_to(ostream& o)const {o<<一种; }
};

还请记住,像这样公开成员变量可能会破坏封装并且通常会被皱眉。 / p>

This might have been asked a million times before or might be incredibly stupid but why is it not implemented?

class A
{
      public:
             A(){ a = 5;}
             int a;
};

class B:public A
{
      public:
             B(){ a = 0.5;}
              float a;
};

int main()
{
    A * a = new B();

    cout<<a->a;
    getch();
    return 0;
}

This code will access A::a. How do I access B::a?

解决方案

To access B::a:

cout << static_cast<B*>(a)->a;

To explicitly access both A::a and B::a:

cout << static_cast<B*>(a)->A::a;
cout << static_cast<B*>(a)->B::a;

(dynamic_cast is sometimes better than static_cast, but it can't be used here because A and B are not polymorphic.)

As to why C++ doesn't have virtual variables: Virtual functions permit polymorphism; in other words, they let a classes of two different types be treated the same by calling code, with any differences in the internal behavior of those two classes being encapsulated within the virtual functions.

Virtual member variables wouldn't really make sense; there's no behavior to encapsulate with simply accessing a variable.

Also keep in mind that C++ is statically typed. Virtual functions let you change behavior at runtime; your example code is trying to change not only behavior but data types at runtime (A::a is int, B::a is float), and C++ doesn't work that way. If you need to accommodate different data types at runtime, you need to encapsulate those differences within virtual functions that hide the differences in data types. For example (demo code only; for real code, you'd overload operator<< instead):

class A
{
  public:
         A(){ a = 5;}
         int a;
         virtual void output_to(ostream& o) const { o << a; }
};

class B:public A
{
  public:
         B(){ a = 0.5;}
         float a;
         void output_to(ostream& o) const { o << a; }
};

Also keep in mind that making member variables public like this can break encapsulation and is generally frowned upon.

这篇关于为什么C ++没有虚拟变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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