-2

Consider this snippet of code.

class A{
    public:
    A(): a(10) {}
    int a ;
};
class B{
public:

    B() : A::A(){}
    A a;

};

Compiler gave me a warning

error: type 'A' is not a direct base of 'B'|

I understand the error but how i can call A's constructor in B's constructor. Any alternative.

Ik it's an implicit call but i need an explicit call. As compiler says.

warning: 'B::a' should be initialized in the member initialization list [-Weffc++]|

P.S. - Don't recommend INHERITANCE. I can't do that.

Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
  • For what instance of `A` do you want to call it? For `a`? Then it's `B() : a() {}` (which is equivalent to just `B() {}`). – HolyBlackCat Nov 15 '19 at 14:48
  • 2
    `a`s constructor is already called implicitly – 463035818_is_not_an_ai Nov 15 '19 at 14:48
  • @formerlyknownas_463035818 I want to call it explicitly in B's initializer list. – Nikhil Badyal Nov 15 '19 at 14:49
  • 3
  • 1
    [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) why do you think you need to call the constructor of `A` ? – 463035818_is_not_an_ai Nov 15 '19 at 14:49
  • btw the term "eclosing class" is used for something else. Here `a` is simply a member of `B` – 463035818_is_not_an_ai Nov 15 '19 at 14:50
  • 1
    @formerlyknownas_463035818 It's not XY problem. I have enabled compiler warning and it says to initialize it in B's initializing list. And i found that irritating. – Nikhil Badyal Nov 15 '19 at 14:51
  • Which compiler (and which version) gives such warning? The compiler should initialize `a` with default constructor without any initializer list (and it should certainly not warn programmer about not doing what it is supposed to do). – Yksisarvinen Nov 15 '19 at 14:53
  • @formerlyknownas_463035818 already told you compiler is suggesting me to do so. This is what is says it's gcc` 'B::a' should be initialized in the member initialization list [-Weffc++]|` – Nikhil Badyal Nov 15 '19 at 14:53
  • "I have enabled compiler warning and it says to initialize it in B's initializing list. And i found that irritating." exactly that is missing from your question to know what problem you are trying to solve – 463035818_is_not_an_ai Nov 15 '19 at 14:53
  • 1
    @Problematic "_ I have enabled compiler warning and it says to initialize it in B's initializing list._" 1) I feel that you misunderstood the warning. If you are asking such question, because of a warning, provide the warning in your question, and we'll help you solve the warning. 2) Your such comment revealed, that it is, in fact, an XY problem. – Algirdas Preidžius Nov 15 '19 at 14:54
  • @AlgirdasPreidžius check comment – Nikhil Badyal Nov 15 '19 at 14:55
  • calm down (i did already ;). I was too fast in writing one comment (deleted by now). It is a xy problem and deniying it wont help anybody. The warning should be part of the question, because otherwise one has to wonder what the question is about – 463035818_is_not_an_ai Nov 15 '19 at 14:56
  • @formerlyknownas_463035818 Added :-) – Nikhil Badyal Nov 15 '19 at 14:57
  • @Yksisarvinen There are C++ guidelines that mandate initializing all member objects either inline or in the member initializer list. Our linter at work enforces this, for example. – Max Langhof Nov 15 '19 at 15:03
  • @MaxLanghof That's fine, but I don't know why it's part of the "effective C++" set of warnings. Compiler is obliged to initialize this member so it's neither inefficient nor dangerous to not do that. I guess it's just a part of the guidelines by Scott? It seems that [I'm not the only one who finds the behaviour of this flag weird](https://stackoverflow.com/questions/11496942/understanding-weffc) – Yksisarvinen Nov 15 '19 at 15:11
  • @Yksisarvinen One could argue that it prevents bugs due to forgetting to actually pass arguments to (default-constructable) base classes or members. I'm pretty sure it's caught the occasional problem for me. But it also creates verbosity. Whether this tradeoff is overall beneficial is debatable. – Max Langhof Nov 15 '19 at 15:15

1 Answers1

2

It seems you mean

class B{
public:

    B() : a(){}
    A a;

};

Though it is redundant because in any case the data member a will be default initialized by using the default constructor of the class A.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335