0

I was looking at this microsoft article on abstract classes. The article says

Abstract classes cannot be used for:

• Variables or member data
• Argument types
• Function return types
• Types of explicit conversions

If I understand correctly, this quote means that Abstract class type cannot be a member of another class, argument of another function or return type of another function.

What is the meaning of

Types of explicit conversions

Does this mean that we cannot typecast something like (BaseClass)obj1 where obj1 is of another class type? If this understanding is true, when casting one object to another class type may be necessary?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • You cannot cast to abstract class. This might help with second part of your question: https://stackoverflow.com/questions/8622258/type-casting-to-an-abstract-class – Hameed Oct 30 '17 at 05:41
  • 1
    All of the restrictions listed stem from the fact that an abstract class cannot be instantiated. A cast or other type conversion will result in an instance, even if temporary. You can define a pointer to one, but it's really pointing to a child that is not abstract. Or to nothing or it hasn't been initialized. But it's not pointing to an instance of abstract class. – user4581301 Oct 30 '17 at 06:13

2 Answers2

0

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. The following is an example of an abstract class:

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. For example, the compiler will not allow the following:

struct A {
  virtual void g() { } = 0;
};

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. The following example demonstrates this:

struct A {
  virtual void f() = 0;
};

struct B : A {
  virtual void f() { }
};

// Error:
// Class A is an abstract class
// A g();

// Error:
// Class A is an abstract class
// void h(A);
A& i(A&);

int main() {

// Error:
// Class A is an abstract class
//   A a;

   A* pa;
   B b;

// Error:
// Class A is an abstract class
//   static_cast<A>(b);
}
Muhammad Faizan
  • 1,709
  • 1
  • 15
  • 37
0

It means that you can't do something like this:

class base
{
public:
    virtual void foo() = 0;
};

class derived : public base
{
public:
    void foo()
    {
    }
};

int main()
{

    derived pDerived;
    static_cast<base>(pDerived); // Error
}

If base class is not abstract you will be able to compile this.

Konstantin T.
  • 994
  • 8
  • 22