Home  >  Article  >  Backend Development  >  C++ syntax error: virtual members cannot be static or non-static data members. What should I do?

C++ syntax error: virtual members cannot be static or non-static data members. What should I do?

PHPz
PHPzOriginal
2023-08-22 09:52:441021browse

In C, when we use the virtual keyword to define a function member, it means that the function is a virtual function and can be overridden by subclasses. However, if we define a data member (i.e. attribute) as virtual, the C compiler will report an error: virtual members cannot be static or non-static data members.

So why can't virtual be used for data members? Because the difference between virtual functions and non-virtual functions is that virtual functions are accessed through the function table, while data members are accessed directly. If we define the data member as virtual, the program will error because it cannot find the virtual function.

So what if you really need to override a member variable in a subclass? You can use a new feature introduced in C11 - virtual inheritance. Virtual inheritance is a special inheritance method that can reduce the complexity of multiple inheritance and solve some problems. In virtual inheritance, the constructor of the base class will only be called once, thus avoiding the problems caused by multiple initializations.

The following is a sample code that uses virtual inheritance to implement overriding member variables:

class Base {
public:
    virtual int& getVar() { return var; }

protected:
    int var;
};

class Derived : virtual public Base {
public:
    virtual int& getVar() { return var; }

protected:
    int var;
};

int main() {
    Base* b = new Derived();
    b->getVar() = 42;
    cout << b->getVar() << endl;

    return 0;
}

In this sample code, we define a base class Base and a derived class Derived. There is an integer variable var in the Base class. We define it as a virtual type. Although this variable cannot be accessed in this way, this function can be used as a base class for rewriting by derived classes. The Derived class also has an integer variable named var, and it overrides the getVar() function in Base, covering the function in Base. In the main function, we create an instance of Derived, point to it with the base class pointer b, and access it through the getVar() function. The output is 42.

Using virtual inheritance, we can override the member variable named var in the base class in the derived class, and can access the value of this variable, achieving an operation similar to overriding a function.

In summary, the virtual keyword in C can only be used for functions, not data members. If you want to override a member variable in a subclass, you can use virtual inheritance. Virtual inheritance can avoid the problem of multiple initialization and can access overridden member variables in the base class.

The above is the detailed content of C++ syntax error: virtual members cannot be static or non-static data members. What should I do?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn