pure-virtual

Pure virtual destructor in C++

*爱你&永不变心* 提交于 2019-11-26 00:16:34
问题 Is it wrong to write: class A { public: virtual ~A() = 0; }; for an abstract base class? At least that compiles in MSVC... Will it crash at run time? 回答1: Yes. You also need to implement the destructor: class A { public: virtual ~A() = 0; }; inline A::~A() { } should suffice. And since this got a down vote, I should clarify: If you derive anything from A and then try to delete or destroy it, A 's destructor will eventually be called. Since it is pure and doesn't have an implementation,

pure virtual function with implementation

戏子无情 提交于 2019-11-25 23:46:04
问题 My basic understanding is that there is no implementation for a pure virtual function, however, I was told there might be implementation for pure virtual function. class A { public: virtual void f() = 0; }; void A::f() { cout<<\"Test\"<<endl; } Is code above OK? What\'s the purpose to make it a pure virtual function with an implementation? 回答1: A pure virtual function must be implemented in a derived type that will be directly instantiated, however the base type can still define an

How do you declare an interface in C++?

为君一笑 提交于 2019-11-25 22:59:26
问题 How do I setup a class that represents an interface? Is this just an abstract base class? 回答1: To expand on the answer by bradtgmurray, you may want to make one exception to the pure virtual method list of your interface by adding a virtual destructor. This allows you to pass pointer ownership to another party without exposing the concrete derived class. The destructor doesn't have to do anything, because the interface doesn't have any concrete members. It might seem contradictory to define a