Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class init in the initialization list

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...
}
like image 275
dev_eng Avatar asked Dec 30 '25 16:12

dev_eng


2 Answers

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.

like image 109
Serge Ballesta Avatar answered Jan 02 '26 04:01

Serge Ballesta


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.)

like image 37
MattTT Avatar answered Jan 02 '26 05:01

MattTT