Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:23:31+00:00 2026-06-15T13:23:31+00:00

I’m trying to write an application with 4 viewpoints. I’d like to have a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:23:32+00:00Added an answer on June 15, 2026 at 1:23 pm
    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?

    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 for viewport 2,3,4:

    0016FBD3
    0016FBD3
    0016FBD3
    0016FBD3
    

    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.

    1. 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?

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.