Call from Class created inside a Class

Hey guys !

I have 2 Classes.
-> StateManager
-> Intro

The StateManager creates the Intro. I want that the Intro calls a function of the StateManager if finished. How can I achieve that?

At line 24 at the Intro class you can see what I tried.

StateManager:
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
38
39
40
41
42
43
44
45
46

#pragma once

#include "State.h"
#include "Intro.h"

class StateManager{

private:

	std::vector <State*> States;

public:

	StateManager(){
		States.push_back(new Intro("Intro"));
	}

	~StateManager(){
		States.clear();
	}

	//Process all states
	void process(){
		for(int i=0;States.begin()+i<States.end();i++){
			States[i]->process();
		}
	}

	//Draws all states
	void draw(sf::RenderTarget& target) {
		for(int i=0;States.begin()+i<States.end();i++){
			States[i]->zeichne(target);
		}
	}

        //Add new state
        void addState(sf::String name){

          switch (name){
           case "Menu": {States.pushback(new Menu("Menu"));break;}
          }
        }

}theStateManager;


Intro:
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
#pragma once

class Intro : public State{

private:

	sf::String  s_Name;
	bool finished;

public:

	Intro(const sf::String name) : s_Name  (name), finished (false) {
	}

	//Draws the intro
	void draw(sf::RenderTarget& target) const{	

	}

	//Process the intro
	void process(){

	  // >>> Thats what I tried <<<
          if(finished)theStateManager.addState("Menu");	

	}

	//Gives the name of the state
	sf::String giveName() {
		return  s_Name;
	}

};


Thanks for your help!

Grettings
Er4zoX
Last edited on
Store a pointe to the manager class in your state class.
How can I do that?
add a ponter member to your state|intro class and pass it in the constructor.
But how do I pass a pointer of the class itself to the constructor?
1
2
3
	StateManager(){
		States.push_back(new Intro("Intro", this));
	}
FOr example
Topic archived. No new replies allowed.