zoukankan      html  css  js  c++  java
  • How to solve 'object of abstract class type "newFoo" is not allowed'?

     How to solve 'object of abstract class type "newFoo" is not allowed' and C2259 (cannot instantiate abstract class) error? 



    Abstract classes (C++ only)

    An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

    class AB {
    public:
    virtual void f() = 0;
    };




    Function AB::f is a pure virtual function. A function declaration cannot have both a pure specifier and a definition.

    You cannot use an abstract class as a parameter type, a function return type, or the type of an explicit conversion, nor can you declare an object of an abstract class. You can, however, declare pointers and references to an abstract class.

    Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class.
    For example:

    class AB {
    public:
    virtual void f() = 0;
    };

    class D2 : public AB {
    void g();
    };

    int main() {
    D2 d;
    }



    The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f()from AB. The compiler will allow the declaration of object d if you define function D2::g().

    Note that you can derive an abstract class from a non-abstract class, and you can override a non-pure virtual function with a pure virtual function.

    A base class containing one or more pure virtual member functions is called an abstract class.

    • The members of a class declared with the keyword class are private by default. A class is inherited privately by default.
    • The members of a class declared with the keyword struct are public by default. A structure is inherited publicly by default.
    • The members of a union (declared with the keyword union) are public by default. A union cannot be used as a base class in derivation.


    annot allocate an object of abstract type

    This indicates you've not implemented all the pure virtual functions in the derived class. So first, implement all the pure virtual functions, then create instance of this class.

    You cannot create instances of class which has even a single pure virtual function!

    Remember, pure virtual functions are those which are assigned with zero, as

    class sample
    {
    public:
         virtual void f(); //virtual function
         virtual void g()=0; //pure virtual function
    };
    

    Here only g() is pure virtual function! This makes sample an abstract class, and if the derived class doesn't define g(), it would also become an abstract class. You cannot create instance of any of these class, as both of them are abstract!



    With the following codes, I got an intellisense error of "object of abstract class type is not allowed" at "new newFoo();" 
    line.   #include "stdafx.h"
    #include <Windows.h>
    
    class foo
    {
              public: 
                  static HRESULT get_Id(WCHAR** get_IdResult); 
                  virtual HRESULT Initialize() abstract;
    
    };
    
    class newFoo: foo
    {
    	public:
                  virtual HRESULT Initialize(WCHAR* id);
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        WCHAR x[] = L"Hi"; 
        newFoo* obj = new newFoo();           // Got 'object of abstract class type is not allowed' intellisense error. 
        obj->Initialize(x); 
    
    	return 0;
    }
    

     

    If I build this C++ project, I got C2259: 'newFoo': cannot instantiate abstract class.

    How can I resolve this issue?


    first your newFoo class is abstract since you did not implement the Initialize method. Second, it is not a good idea to override and overwrite a method at the same time. Your Initialize method of newFoo does so. So you will have to make the base class Initialize method non abstract or to implement it in the newFoo class.

    Beware of not getting an signature lookup error if you really want to override and overwrite Initialize in newFoo, this is an issue on how the compiler looks up for the signature of your Initialize method. Let's assume the Initialize method of foo is not abstract:

    newFoo* obj = new newFoo();
    obj->Initialize( ); 
    

     

    In this code we want to call the Initialize method without arguments but it will result in an error.

    First the compiler looks in the concrete class newFoo and finds a method with the appropriate name, then the compiler continues to look up for the right signature but will not find one. To enhance the lookup for the compiler you will have to use the using declaration:

    class newFoo: foo
    {
    	public:
                  using foo::Initialize;
                  virtual HRESULT Initialize(WCHAR* id);
    };

     

    You can read more about this here: http://msdn.microsoft.com/en-US/library/was37tzw(v=VS.100).aspx



  • 相关阅读:
    常用的学生、课程、成绩、教师表的查询
    flutter 自定义TabBar
    Flutter 设置input边框
    Flutter ReorderableListView 可拖拽的列表
    Flutter NotificationListener 监听列表的滚动
    Flutter 使用Tabbar不要Title
    Nestjs 验证对象数组
    postman 发送数组
    JS面向切面编程AOP
    什么是匿名函数、什么是闭包函数?
  • 原文地址:https://www.cnblogs.com/yefengmeander/p/2887937.html
Copyright © 2011-2022 走看看