Constructors – Derived to Base

Constructors – Derived to Base

My base class contains this constructor:

class Parent {  Parent(int a, int b, char* c);};

My derived class contains this constructor:

class Child : public Parent {  Child(int a);};

defined as

Child::Child(int a) : Parent(a,2){  .. some code}

I need a third class derived from class Childclass Grandchild :

public Child {  Grandchild(int a);};

but I can’t figure out how to define the constructor. I’ve tried:

Grandchild::Grandchild(int a):Child (a,2)Grandchild::Grandchild(int a):Parent(a,2)Grandchild::Grandchild(int a):Child(a):Parent(2)Grandchild::Grandchild(int a):Child(a),Parent(2)

You can’t initialize a non-immediate base class in a derived class (virtual base classes are an exception to this rule). What you need is to have class GrandChild initialize Child, and delegate the responsibility of initializing Parent to Child’s constructor:

class Grandchild : public Child { Grandchild(int a):Child (a,2){}//implicit init of Parent};

Child’s constructor automatically initializes Parent with the values a and 2.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved