开发者

why our C++ class is forced abstract?

开发者 https://www.devze.com 2023-02-03 17:52 出处:网络
Why when we inherit an abstract C# class defined in a .dll, our C++-CLI class is forced abstract? is there any 开发者_运维问答way to make it not abstract our c++-CLI class?I\'m not sure I understood y

Why when we inherit an abstract C# class defined in a .dll, our C++-CLI class is forced abstract?

is there any 开发者_运维问答way to make it not abstract our c++-CLI class?


I'm not sure I understood your question correctly, but you have to implement all the methods declared but not implemented in the abstract base class, otherwise naturally your class is considered abstract as well.


If your class declares any methods that are "pure virtual" (i.e., with "= 0"), or inherits any pure virtual methods without overriding them, your class will be abstract.


class A
{
public:
    virtual void fun() = 0; //"pure" virtual function
    virtual void gun() = 0; //"pure" virtual function

    virtual void sun() //virtual function - "not pure"
    {
      /***** implementation code *****/
    }
};

Here A is an abstract class, since it has two pure virtual functions.

class B : public A
{
public:
    virtual void fun() 
    {
       /******* implementation code ********/
    }
};

Question: What is B? Is it non-abstract? After all, it has defined/implemented one pure virtual function called fun()?

Answer : No. B too is an abstract class, because it didn't define/implement gun() which too is pure virtual function in the base class A. Defining just one pure virtual function called fun() doesn't make it non-abstract. It has to define ALL pure virtual functions. ALL means ALL, as many as there are pure virtual functions.

So two important notes here : 1) pure virtual functions, 2) define all of them. Only then a class can become concrete/non-abstract!

0

精彩评论

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

关注公众号