-1

I need to use default parameters for an inherited class's constructor.

class A
{
public:
    A(int data)
    {
        a_data = data;
    }
    ~A(){}

    int a_data; 
};


class B : public A
{
public:
    B(int in, int num = DATA) : A(num)
    {
        b_data = in;
    }
    const int DATA = 9;
    int b_data;
};

int main()
{
    B b(3);
    int x = b.a_data;

    return 0;
}

I've found here that I can use a default parameters for a child's class constructor. But it is said that we should use a global parameter.

But I use a data from class "B" so the A constructor is called before this data is released. In the result I get some garbage in "x" but not 9.

! So, the problem was extracted from another, more particular one. I have a basic class "Object". And some children like "Mob" or "Player". Object has a physical (Box2D) object inside. Certanly, all the actions creating that physical body are the same, except of some parametres. So, I store algorythm in "Object" construtor, and put all the needed data in child classes. But, I can't reach this data, before creating an "Object" which requires that data.

Here is an example of the solution for one dependent class.

I would appreciate any help.

8
  • Could someone explain why the version above still compiles, but does not produce 9? Does the compiler just ignore cost int num = 9 when constructing A? Commented May 24, 2015 at 0:20
  • @rivanov the compiler first constructs the Base part of a Derived object, and only after it initializes the other members of the Derived object. When it tries to construct the Base, the members of the Derived object are not yet initialized. Commented May 24, 2015 at 1:26
  • @vsoftco Ah I see, so what you're saying is, when B(int in) : A(num) is called, this is executed before const int num = 9, which is 0 by default. Thus A(num) is always called with 0. Is this a correct interpretation? Commented May 24, 2015 at 1:30
  • @vsoftco, here is an example of solution, but as I understand it wouldn't work for a structure with muiltiple children classes? Commented May 24, 2015 at 1:31
  • @rivanov the num is not even 0, it is uninitialized. It happens to be zero in some cases, but in general trying to use un-initialized variables is undefined behaviour. Commented May 24, 2015 at 1:35

2 Answers 2

1

Use a default value constructor such as:

B(int in, int num = 9) : A(num) {
    b_data = in;
}

This works as shown here.

Sign up to request clarification or add additional context in comments.

8 Comments

I already tried this variant, but I get an error: "a non static member must be relative to a specific object"
@vsoftco: Thank you for your comment; means something from a >10k user such as yourself.
@Entrack: I've literally copy pasted your code and made that change; it compiles and works as shown in the link. If there is an error, it is because of something else in your code that is different than what is shown in the question.
Oh, sorry, I mean if you put this into the constructor you get an error B(int in, int num = num) : A(num) { b_data = in; }
@Entrack: Ah, no that's not going to work, you have to put an actual value there int num = 9 should work if that's the default value you want to construct it with. Otherwise, use a different value and ignore the const int variable altogether.
|
0

What you want is to provide a default constructor for A

A(int data = 9)

so B can create by default As that have 9 as their parameter,

B(int in){...} // here the A part is constructed with 9

Your actual code doesn't do what you expect since the constructor of B first tries to construct the base part (in this case A), and only after initialized the rest of B.

4 Comments

Basicly, no. I need 2 parametres to create B. "in" and "num" -- default one
@Entrack See the updated edit. Otherwise, you'd need a global variable, which is not the way to go.
You see, if I have more child classes whith individual parametres, it would be incorrect
@Entrack see the R_Kapp's answer, as I think it's better than mine and I think does exactly what you want. If it's still not satisfactory, maybe you should think about changing the design ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.