开发者

Figuring out what makes a C++ class abstract in VS2008

I'm using VS2008 to build a plain old C++ program (not C++/CLI). I have an abstract base class and a non-abstract derived class, and building this:

Base* obj;
obj = new Derived();

fails with the error "'Derived': cannot instantiate abstract class". (It may be worth noting, however, that if I hover over Base with the cursor, VS will pop up a tooltip saying "class Base abstract", but hovering over Derived will only say "class Derived" (no "abstract")).

The definitions of these classes are fairly large and I'd like to avoid manually checking if each method has been overridden. Can VS do t开发者_C百科his for me somehow? Any general tips on pinpointing the exact parts of the class' definition that make it abstract?


The compiler should tell you in the error message. The following:

struct base
{
    virtual void foo(void) = 0;
    virtual void bar(void) = 0;
};

struct derived : base
{
    virtual void foo(void){}
};

int main(void)
{
    derived d;
}

Produces:

error C2259: 'derived' : cannot instantiate abstract class
due to following members:
'void base::bar(void)' : is abstract
see declaration of 'base::bar'

It also does the same with dynamic allocation.


No, I don't believe VS does anything like this out of the box. See GMan's answer.

On the other hand, if you've got a base class which is so large you can't quickly check it's virtual methods, you probably need to think about breaking up that class.


What GMan said. Plus use a better compiler :-) With g++, the error is:

ab.cpp: In function 'int main()':
ab.cpp:14: error: cannot declare variable 'd' to be of abstract type 'derived'
ab.cpp:8: note:   because the following virtual functions are pure within 'derived':
ab.cpp:4: note:         virtual void base::bar()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜