Hi again. I'm going through the SFML tutorial on github.com named "Manage different screens in a game", which can be found here:
https://github.com/SFML/SFML/wiki/TutorialScreensWould appreciate it if someone can explain how the following code works:
// Screen.hpp
class cScreen
{
public :
virtual int Run (sf::RenderWindow &App) = 0;
};
----------
// Main.cpp
01 #include <fstream>
02 #include <iostream>
03 #include <sfml/Graphics.hpp>
04 #include "screens.hpp" // Includes screen.hpp, screen_0.hpp and screen_1.hpp
05
06 int main(int argc, char** argv)
07 {
08 //Applications variables
09 std::vector<cScreen*> Screens;
10 int screen = 0;
11
12 //Window creation
13 sf::RenderWindow App(sf::VideoMode(640, 480, 32), "SFML Demo 3");
14
15 //Mouse cursor no more visible
16 App.ShowMouseCursor(false);
17
18 //Screens preparations
19 screen_0 s0;
20 Screens.push_back (&s0);
21 screen_1 s1;
22 Screens.push_back (&s1);
23
24 //Main loop
25 while (screen >= 0)
26 {
27 screen = Screens[screen]->Run(App);
28 }
29
30 return EXIT_SUCCESS;
31 }
(For more details on "all" of the code, please refer to the link given.)
Line 09 is: std::vector<cScreen*> Screens;
I understand that it's instantiating a vector named Screens. What I don't understand is how the cScreen* pointer type is used.
The class screen_0 inherits from the class cScreen. Similarly, the class screen_1 inherits from the class cScreen. But the code in screen_0 is different from the code in screen_1. So when line-09 instantiates the Screens vector using <cScreen*>, how does the vector know the size of screen_0 and screen_1 since they are both different sizes?
I assume that when screen_0 and screen_1 are push_back'd into the Screens vector, the vector just logs the starting address of each push_back and the actual size of each element is irrelevant. Is that correct? If so, then why does the "std::vector<cScreen*> Screens" need to know what type of pointer cScreen is? If it's just a memory address that's being logged, why care that the pointer type is cScreen?
Learning C++ so please excuse my noobie question,
Raptor