Problem with passing parameter to parent class in constructor

I have a class (inheritance is virtual cause I use multiple inheritance ahead):
class XGE_Texture : virtual public XGE_Object
Its constructor (rect() prints the SDL_Rect struct which is basically formed by 4 ints):
1
2
3
4
5
6
XGE_Texture::XGE_Texture(SDL_Texture* sprite, SDL_Rect source, SDL_Rect bounds) : XGE_Object(bounds.x, bounds.y, bounds.w, bounds.h){
	texture = sprite;
	draw_bounds = source;
	std::cout<<"T1: "<<rect(bounds);
	std::cout<<"T2: "<<rect(this->bounds)<<"\n";
}

This is the constructor of XGE_Object:
1
2
3
4
5
XGE_Object::XGE_Object(int x, int y, int w, int h){
	std::cout<<"O1: "<<x<<" "<<y<<" "<<w<<" "<<h<<"\n";
	bounds = {x, y, w, h};
	std::cout<<"O2: "<<rect(bounds);
}

So I create an object:
1
2
3
4
5
6
XGE_Texture(NULL, {0, 0, 0, 0}, {300, 200, 200, 200});
// Output:
O1: 0 0 0 0
O2: 0 0 0 0
T1: 300 200 200 200
T2: 0 0 0 0

There must be something wrong with parameter passing in initializer list. Is it possible to pass members of a variable like I did (eg. bounds.x)?
There must be something wrong with parameter passing in initializer list. Is it possible to pass members of a variable like I did (eg. bounds.x)?

Yes.

When asking for help with problems like this it is helpful if you supply a minimal code sample that is compilable and replicates the issue.

What does the following do for you?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// http://ideone.com/9sdSMu
#include <iostream>

struct rect {
    int x, y, w, h;
};

std::ostream& operator<<(std::ostream& os, rect r)
{
    return os << r.x << " " << r.y << " " << r.w << " " << r.h << '\n';
}

struct A
{
    rect bounds;

    A(int x, int y, int w, int h)
    {
        std::cout << "O1: " << x << " " << y << " " << w << " " << h << "\n";
        bounds = { x,y,w,h };
        std::cout << "O2: " << rect(bounds);
    }
};

struct B : A
{
    B(rect bounds) : A(bounds.x, bounds.y, bounds.w, bounds.h)
    {
        std::cout << "T1: " << rect(bounds);
        std::cout << "T2: " << rect(this->bounds) << "\n";
    }
};

int main()
{
    B b({ 300, 200, 200, 200 });
}
Last edited on
The example you posted works fine, and gives the expected result. I don't know why it's not working in my project... Could it have something to do with the inheritance being virtual?
EDIT:
I tried this:
1
2
3
4
5
6
XGE_Texture::XGE_Texture(SDL_Texture* sprite, SDL_Rect source, SDL_Rect bounds) : XGE_Object(300, 200, 200, 200){
	texture = sprite;
	draw_bounds = source;
	std::cout<<"T1: "<<rect(bounds);
	std::cout<<"T2: "<<rect(this->bounds)<<"\n";
}

And the output is still:
O1: 0 0 0 0
O2: 0 0 0 0
T1: 300 200 200 200
T2: 0 0 0 0
Last edited on
I tried this:

Minimal, compilable code sample which reproduces the problem.
I can't make a compilable sample cause it's a project which involves a library and many other things. It can't be separated.
Last edited on
It looks like that XGE_Object does not set the member bounds
No, instead it does, but it receives the wrong parameters. In my 2nd test I pass to it directly (300, 200, 200, 200) and it prints (0, 0, 0, 0). So the received parameters are wrong, but why?
Show the XGE_Object constructor. The passed parameter cannot be used to initialized the member bounds.
The constructor is this one:
1
2
3
4
5
XGE_Object::XGE_Object(int x, int y, int w, int h){
	std::cout<<"O1: "<<x<<" "<<y<<" "<<w<<" "<<h<<"\n";
	bounds = {x, y, w, h};
	std::cout<<"O2: "<<rect(bounds);
}

At O1 it prints 0, 0, 0, 0 so it's not even receiving the correct parameters...
Topic archived. No new replies allowed.