Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C++ abstract classes incomplete types?

Incomplete types cannot be instantiated, and abstract classes (that is, those with pure virtual member functions) also cannot be instantiated.

struct incomplete_type;

struct abstract_class
{
   virtual void foo() = 0;
};

By any chance are abstract classes themselves considered to be incomplete types?

like image 519
Lightness Races in Orbit Avatar asked May 16 '13 20:05

Lightness Races in Orbit


People also ask

What are incomplete types in C?

There are only three variations of incomplete types: void, arrays of unspecified length, and structures and unions with unspecified content. The type void differs from the other two in that it is an incomplete type that cannot be completed, and it serves as a special function return and parameter type.

What is an abstract class C?

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.

Why do we create incomplete method in abstract class and interface?

@locobe: Because you have to implement an abstract method, while you can leave an empty class as it is. The purpose of the adapter classes is to have a default implementation (doing nothing or nothing useful) for the respective interface that allows you to implement only those methods you need.

Can abstract method have body?

Abstract methods cannot have body. Abstract class can have static fields and static method, like other classes.


1 Answers

No!

Superficially they are similar (beyond the fact that abstract classes do have definitions whereas incomplete types do not):

[n3690: 3.9/5]: [..] Objects shall not be defined to have an incomplete type.

[n3690: 10.4/2]: An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it. [..]

[n3690: 10.4/3]: An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion. Pointers and references to an abstract class can be declared.

However, the standard in fact makes clear that the two concepts are unrelated:

[n3690: 3.9/5]: A class that has been declared but not defined, an enumeration type in certain contexts (7.2), or an array of unknown size or of incomplete element type, is an incompletely-defined object type. Incompletely-defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.

Your abstract class has a definition, and therefore cannot be considered to be an incomplete type.


N3690 is the C++14 Committee Draft.

like image 137
Lightness Races in Orbit Avatar answered Sep 19 '22 15:09

Lightness Races in Orbit