Home > Mobile >  Abstract class init in the initialization list
Abstract class init in the initialization list

Time:10-07

I want to understand the following c concept. class_a is abstract class and as per abstract class concept we cannot create any instance of it. i have used the initlization list and abstract class as well but never used following concept. In the code ,the initlization list of class_b, class_a is initlized. I want to understand what is meaning of initlizing it in the initilization list.

 class_b::class_b(int val):nameA::class_a()

in fileA.cpp

namespace nameA
{
class class_a
{
    public:
        virtual ~class_a(){};
        virtual void process()=0;
};
}

in fileb.h file

namespace nameB
{
class class_b  : public nameA::class_a
{
    public:
    class_b(int val);
}
}

in fileb.cpp file

namespace nameB
{
class_b::class_b(int val)
:nameA::class_a() //Want to understand this line...
}

CodePudding user response:

It would be more clear with a slightly richer example. Because if the abstract base class has neither attributes nor methods it is harder to see how it can be initialized.

class NamedProcessor {
    std::string name;    // a private attribute
public:
    NamedProcessor(const std::string &name) : name(name) {}
    virtual ~NamedProcessor() {}

    // a pure virtual method to make the class abstract
    virtual void process() = 0;

    std::string getName() {
        return name;
    }
};

class Doubler : public NamedProcessor {
    int value;           // a private attribute

public:
    Doubler(int initial = 1) : NamedProcessor("Doubler") {
        reset(initial);
    }

    void reset(int initial) {
        value = initial;
    }

    int getValue() {
        return value;
    }

    // the concrete implementation
    void process() {
        value *= 2;
    }
};

int main() {
    // Compiler would croak witherror : variable type 'NamedProcessor' is an abstract class
    // NamedProcessor wrong("Ill formed");

    Doubler doubler;
    std::cout << doubler.getName() << "\n";     // name has been initialized...
    return 0;
}

Here the abstract class holds an attribute which will be available to subclasses. And this attribute has to be set at construction time because there is no public setter for it. The language has to provide a way to initialize the abstract subclass, meaning not building an object, but initializing a sub-object - here setting the name.

CodePudding user response:

By deriving, every class_b object will contain a class_a sub-object.

Even if you cannot instanciate an object of type class_a, there may be a need of initializing this sub-object. Consider that an abstract class can also have members.

If you have an abstract class in terms of an interface (like in Java), this abstract class obviously needs no initialization. Nevertheless it would get a (empty) default constructor in C , that you can call explicitly in the initialization list. (If you do not call it explicitly, it will be called implicitly.)

CodePudding user response:

If class is abstract it doesn't mean that it can't have any constructor. It means that you can't use it for creating an independent object.

So here is classic inheritance mekanism:

  • You creating a child_class object
  • It calls a child_class() constructor
  • child_class() constructor calls base_class() constructor to garantue that fields of base_class are built correct
  • Then executes child_class() constructor for the rest child_class fields

In your example you call class_a() constructor by yourself, but it will be called anyway. So in sum, it's not about abstract class, it's all about simple inheritance.

You need to have some mekanism for initializing class_a fields if they exists, thats why you can call class_a() constructor even if it's abstract class, otherwise inheritance mekanism just useless.

  • Related