Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a pure abstract class contain static constants, static methods or non-virtual destructors?

Is this a pure abstract class?

class C
{
public:
    static const std::string S;
    C() {}
    virtual ~C() {}
    virtual void v() = 0;
}

I believe it is not, following this definition from WikiBooks:

A pure Abstract class has only abstract member functions and no data or concrete member functions.

It can't be pure abstract because it does not have only abstract member functions:

  1. It has a virtual destructor, which is a member function, but not abstract. It is not a pure destructor. Note that "abstract method" and "pure virtual function" are the same things, and that the term "method" and "function" are synonymous.
  2. It has an attribute S, which represents data.

Now my teachers claim it is a pure abstract class, because:

Constants included in a pure virtual class are not considered attributes. They are immutable elements of a class and therefore they don't violate its abstractness. The same holds for static methods.

like image 436
Slazer Avatar asked Oct 18 '15 12:10

Slazer


People also ask

Can abstract class contains static methods?

Yes, of course you can define the static method in abstract class. you can call that static method by using abstract class,or by using child class who extends the abstract class. Also you can able to call static method through child class instance/object.

Does pure abstract class need virtual destructor?

Pure Virtual Destructors in C++ The only difference between Virtual and Pure Virtual Destructor is, that pure virtual destructor will make its Base class Abstract, hence you cannot create object of that class. There is no requirement of implementing pure virtual destructors in the derived classes.

Can abstract class have non virtual methods?

Abstract classes (apart from pure virtual functions) can have member variables, non-virtual functions, regular virtual functions, static functions, etc.

Can pure virtual class have static method?

In C++, a static member function of a class cannot be virtual.


2 Answers

An abstract class is one in which there is a declaration but no definition

A pure abstract class in C++ is considered as an interface, too.

Hence, any constant declarations doesn't violate the purity of class abstractness. In IDL you can declare constants inside and outside of interfaces.

However, static members, constructors (even empty one), and a non-abstract destructor break the purity. So pure abstract class would looks like following

class C
{
public:
    const std::string S = "Message";
    virtual ~C() = 0;
    virtual void V() = 0;
};
like image 158
serge Avatar answered Sep 21 '22 05:09

serge


The C++ standard doesn't define what is a "pure abstract" class, nor is there any commonly accepted language-independent definition that fits all languages equally.

The definition 1 you quote doesn't make a lot of sense to me in the context of C++. What your teachers claim is more consistent with the language design. Use whatever definition is convenient and necessary. In any case the notion itself is not at all important.

like image 32
n. 1.8e9-where's-my-share m. Avatar answered Sep 20 '22 05:09

n. 1.8e9-where's-my-share m.