I’m trying to write an application with 4 viewpoints. I’d like to have a scene object, what is referenced by all viewports. What I’d like to do is that if the scene object changes, all viewpoints get updated. Here is how I’m trying to implement it now:
#include <iostream>
#include <vector>
using namespace std;
class Scene
{
};
class Viewport
{
public:
Viewport( Scene &scene );
void draw();
private:
Scene *mScene;
};
Viewport::Viewport( Scene &scene )
{
cout << mScene << endl;
cout << &scene << endl;
mScene = &scene;
cout << mScene << endl;
cout << &scene << endl << endl;
}
void Viewport::draw()
{
cout << &mScene << endl;
}
int main( int argc, char *argv[] )
{
Scene mScene;
vector<Viewport> mViewPorts;
mScene = Scene();
cout << "init: " << endl;
for( int i = 0; i < 4; i++ )
mViewPorts.push_back( Viewport( mScene ) );
cout << "main: " << endl;
cout << &mScene << endl << endl;
cout << "draw: " << endl;
for( int i = 0; i < 4; i++ )
mViewPorts[i].draw();
}
My problem is the following
1. Right now, the first viewport initializes an empty scene pointer with address CCCCCCCC. This get’s updated to the main scene’s address. This is what I’m trying to achieve.
CCCCCCCC <- mScene before
0016FBD3 <- &scene before
0016FBD3 <- mScene after
0016FBD3 <- &scene after
The other viewpoints however already start on the correct address, not on CCCCCCCC. Why is this happening?
It looks like this for viewport 2,3,4:
0016FBD3
0016FBD3
0016FBD3
0016FBD3
2. My main problem however is that my concept doesn’t work. When I’m calling draw(), the viewports all have a totally different address than the one I’ve set it before.
draw:
00398730
00398734
00398738
0039873C
Why are these happening, and what is the right way to solve the above problem?
You print mScene before you assign a value to it, so whatever garbage value was sitting around in memory is displayed – evidently it happened to be CCCCCCCC, but if you changed your program a little bit here or there, or changed compiler or optimisation level, you might get a completely different value. You might even get a different value each time you run the program, or the program could crash. It is undefined behaviour to read a value from a variable that has not been set. Simply, your code
cout << mScene << endl;needs to read from mScene to print it, which is ok after the=assignment but not before.It looks like this because as you construct each temporary Viewport to pushback them, they’re almost certainly be constructed in the same place in the program’s stack… the memory content that was initially CCCCCCCC has been overwritten by values the previous time that memory was used for the previous Viewport object, but because the value is read without being reset to anything, you see the old value from the earlier Viewport.
Each of the viewport objects contains a pointer to your scene. You only have one scene, but the four viewports have four distinct pointers at the memory addresses shown, all containing the same address of your single scene.
If you print mScene instead of &mScene you’ll see the value you expect.