开发者

can a class have virtual data members?

开发者 https://www.devze.com 2023-01-16 02:39 出处:网络
class Base{ public: void counter(); .... } class Dervied: public Base{ public: .... } void main() { Base *ptr=new Derived;
class Base{  
    public:  
        void counter();   
    ....   
}

class Dervied: public Base{  
    public:  
        ....  
}

void main()  
{  
     Base *ptr=new Derived;  
     ptr->counter();  
}

To identify that the base class poin开发者_运维知识库ter is pointing to derived class and using a derived member function, we make use of "virtual".

Similarly, can we make derived data members "virtual"? (the data member is public)


virtual is a Function specifier...

From standard docs,

7.1.2 Function specifiers Function-specifiers can be used only in function declarations. function-specifier: inline virtual explicit

So there is nothing called Virtual data member.

Hope it helps...


No, but you can create a virtual function to return a pointer to what you call virtual data member


No, in C++ there are no virtual data members.


I think not, but you might simulate it using virtual getters and setter perhaps?


To identify that the base class pointer is pointing to derived class and using a derived member function, we make use of "virtual".

That is not correct. We make virtual functions to allow derived classes to provide different implementation from what the base provides. It is not used to identify that the base class pointer is pointing to derived class.

Similarly, can we make derived data members "virtual"? (the data member is public)

Only non static member functions can be virtual. Data members can not be.

Here's a link with some more info on that


No, because that would break encapsulation in a myriad of unexpected ways. Whatever you want to achieve can be done with protected attributes and/or virtual functions.

Besides, virtual functions are a method of dispatch (i.e. selecting which function is going to be called), rather than selecting a memory location corresponding to the member attribute.


A class cannot have a virtual member, see for instance this answer. However, you can have something similar using pointers, inheritance and runtime polymorphism.

In the following snippet I define the prototype for a geometrical shape, that has an area method. The picture class has a member shape* s; and the methods of that shape pointed by s are used by picture::show(). In this setup it is undesirable to have an instance of picture before an actual implementation of a shape has been given, hence we force picture to be abstract by adding a dummy virtual function picture::make_real().

// prototypes
class shape
{
    public:
    virtual double area() = 0; // to be defined later
};
class picture
{
    protected:
    shape* s;
    
    virtual void make_real() = 0; // force picture to be abstract
    
    public:
    picture(shape* ptr):
        s{ptr}
    {}
    
    void show()
    {
        std::cout << s->area() << '\n';
    }
};

Next, we actually implement a shape called square and a picture type square_picture that (literally) has a square shape.

// actual implementation

class square : public shape
{
    double len;
    
    public:
    square(double l):
        len{l}
    {}
     
     double area() override
    {
        return len*len;
    }
};



class square_picture : public picture
{
    void make_real() override {} // square_picture is not abstract
    
    public:
    square_picture(double l):
        picture{new square{l}}
    {}
    
    ~square_picture()
    {
        delete s;
    }
};

The class square_picture can be tested with the following snippet

int main()
{
    square_picture A{2.0};
    A.show();
    
    //picture B{nullptr}; // error: picture is abstract
    return 0;
}

which outputs:

4


Maybe you can see the problem in a equivalent way:

class VirtualDataMember{  
    public:  
    ...
}

class DerviedDataMember: public VirtualDataMember{  
    public:  
    ... 
}

class Base{  
    public:  
        VirtualDataMember* dataMember;
        void counter();     
        ...  
}


I have a base class which uses an array of objects. From that class I derived a new class that uses an array of a different type of object. Both variables have exactly the same name. Virtual member functions were added to both classes to process the arrays. These member functions have no trouble finding the correct variable. The member functions and the variables they use are in a common scope.

The virtual member functions are nearly identical in both classes. Only the type of array changed.

C++ templates could have accomplished the same result.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号