Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C++ have virtual variables?

Tags:

c++

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?

like image 879
Bruce Avatar asked Jul 14 '10 16:07

Bruce


People also ask

Are there virtual functions in C?

C has no native syntax for virtual methods. However, you can still implement virtual methods by mimicking the way C++ implements virtual methods. C++ stores an additional pointer to the function definition in each class for each virtual method.

Can we have virtual variables?

A virtual variable is created by applying an operator (a mathematical algorithm) to one or more input acoustic variables (operands). Operands may be raw variables or other virtual variables. Virtual variables include variables (from various variable classes), virtual lines and virtual surfaces.

Why do we need virtual functions in C?

We use virtual functions to ensure that the correct function is called for an object, regardless of the reference type used to call the function. They are basically used to achieve the runtime polymorphism and are declared in the base class by using the virtual keyword before the function.

Can virtual class have member variables?

Abstract classes (apart from pure virtual functions) can have member variables, non-virtual functions, regular virtual functions, static functions, etc.


1 Answers

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.

like image 191
Josh Kelley Avatar answered Sep 19 '22 01:09

Josh Kelley