How to pass variable to a function in constructor

In a project I am working on, I have to initialize a window and pass it as a parameter to another constructor, but before the window is initialized, it is passed as a parameter thus causing an error. Here is some code to give you a better idea of what I am talking about:

1
2
3
4
5
6
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mWorld(mWindow) //<---- right here is where the mWindow variable needs to be passed
{
    //...
}


Is there a way to make this work???
Last edited on
Initialization occurs in the order of declaration in the class.
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
#include <iostream>

class A{
public:
    A(){ std::cout <<"A\n"; }
};

class B{
public:
    B(){ std::cout <<"B\n"; }
};

class C{
    B b;
    A a;
public:
    C(): a(), b(){}
};

class D{
    A a;
    B b;
public:
    D(): a(), b(){}
};

int main(){
    C c;
    D d;
}
Output:
B
A
A
B
Last edited on
Thank you so much Helios, you just solved hours worth of debugging for me!!!
Topic archived. No new replies allowed.