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

  • Home
  • SEARCH
  • 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 7817247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:10:14+00:00 2026-06-02T06:10:14+00:00

I hope that I am providing enough information and that I’ve titled this correctly.

  • 0

I hope that I am providing enough information and that I’ve titled this correctly.

In general, I want to have a class that stores data in my application, and I need several other classes to access the same data. essentially sharing the data between multiple classes.

Short/Concise Code follows:

example.cpp (main application)

// example.cpp : Defines the entry point for the console application.
//
#include "AnotherClass.h"
#include "ObjectClass.h"
#include "stdafx.h"
#include <iostream>  
#include <iomanip>

using namespace std;  
//Prototype
static void do_example ( );

int main()  

{  
    do_example ( );
}

static void do_example ( ) {


    MyObject.a = 5;
    cout <<"MyObject.a value set in main application is "<<MyObject.a<<"\n";

    AnotherClass    m_AnotherClass;

    m_AnotherClass.PrintValue();

}       

ObjectClass.h

class ObjectClass {
public:
    ObjectClass(); // Constructor
    int a; // Public variable
} static MyObject;

ObjecClass.cpp

#include "ObjectClass.h"

ObjectClass::ObjectClass() {

    a = 0;

}

AnotherClass.h

class AnotherClass {
public:
    AnotherClass(); // Constructor

    void PrintValue(); // Public function
    int value; // Public variable

};

AnotherClass.cpp

#include "AnotherClass.h"
#include "ObjectClass.h"
#include "stdafx.h"
#include <iostream>  
#include <iomanip>

using namespace std; 

AnotherClass::AnotherClass() {

    value = MyObject.a;

}

void AnotherClass::PrintValue() {

    cout <<"value in AnotherClass is "<<value<<"\n";
    cout <<"it should be the same."<<"\n";
}

But the value is the default value of 0, as if it is a new instance of MyObject. But it should be pulling the value of 5 from the static MyObject.

What am I missing?

  • 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-02T06:10:16+00:00Added an answer on June 2, 2026 at 6:10 am

    A static class instance is a static variable itself. What you expect to happen also makes sense, however, your code does not show how the static instance is handled. In fact, if both MyClassInstances refer to the same object, then you don’t even need static declaration.

    Also, static variables are defined in cpp files. If you define it in a header, the cpp file (compilation unit) that includes it will define a separate static variable. So, define the static object in the main cpp file and use extern MyStaticClass in the header file. The linker will then link the uses to the same variable.

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

Sidebar

Related Questions

This is my first question here, so I hope that I am providing enough
I hope that title is clear enough. I have 2 projects, MyProject.Website (ASP.NET MVC3
I have this code at http://www.canadaiplawyer.com/ in the hope that the printed version of
Not too sure how to formulate my question and I hope that this is
This is a very strange problem, and I just hope that I can clearly
I downloaded these zip files from the link http://netbeans.org/downloads/zip.html with the hope that this
I have a unique requirement that I hope is possible with LINQ to SQL.
I have a few questions that I hope clarity and facts can be fed
I want to know that can we build a data object using jquery/json, which
We recently started providing a data extract to clients via an XML. We have

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.