1

Quick question: I thought that member initializing lists acted the same as normal initialization using '=' (except for const members which can only be defined using initializing lists), calling the constructors of the to-be-initialized objects with specific arguments that I'm passing, as in my below example (where I'm calling x's constructor with a value of 1). But to my surprize, in the case of a simple inheritance, the compiler complains for not seeing the member I'm trying to initialize with my constructor, although seeing the other one that gets initialized with usual '=' syntax:

#include <iostream> 
using namespace std;

class A
{
public:

    int x;
    int y;
};

class B : public A
{
public:

    B() : x(1)
    {
        y = 2;
    }
};

int main()
{   
    return 0;
}

If you run the above code, you'll see that while y gets detected with no problems, your compiler will say there is no such member named 'x' at the 'B() : x(1)' line. Why is that? The inheritance is public, y gets seen, no problems there, why not x as well?

Otringal
  • 131
  • 7
  • 2
    Possible duplicate of [How can I initialize base class member variables in derived class constructor?](http://stackoverflow.com/questions/7405740/how-can-i-initialize-base-class-member-variables-in-derived-class-constructor) – Evgeny S. Mar 14 '16 at 21:29

2 Answers2

1

You can't initialize x from the initializer list of class B, because only class A can initialize its members.

By the way, making the data members public is not a good idea. Here is an example of how your code would work:

#include <iostream> 
using namespace std;

class A
{
public:
    A(int x, int y) : x(x), y(y) {}
protected:

    int x;
    int y;
};

class B : public A
{
public:

    B() : A(1, 5000)
    {
        y = 2;
    }
};

int main()
{   
    return 0;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • yup, I've already found the solution before seeing your post, it's down below, marked as an answer. Thanks for the help! Yup, I know data should almost never be public, it's just that for example purposes I always make them so, as making them private would require me to also use a setter/getter, which only makes code snippets longer than they should be. However, making them protected in this case would've required no setter/getter, so yeah, in this example, ur right, I was being lazy beyond the point of necessity :) – Otringal Mar 14 '16 at 21:40
  • @Otringal I do not see any answer marked. Anyway, you understood what went wrong and that's what matters. I also upvoted your question, since it showed some effort. – gsamaras Mar 14 '16 at 21:55
  • thanks a lot!!! I've re-marked your answer as "the one", don't know why it didn't stick the first time :/ – Otringal Mar 14 '16 at 22:40
  • 1
    You can't accept your own answer until 2 days are passed @Otringal. Thanks! – gsamaras Mar 14 '16 at 23:09
0

I think I've found the answer:

My objects are already being initialized once when the constructor of class A is being executed, thus I cannot re-initialize it when running B's constructor. Reassigning y to a value of 2 is ok, but re-initializing x with 1 being passed as its constructor argument is the cause of the compiler's error. Am I missing anything else???

Otringal
  • 131
  • 7