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 8260603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:08:12+00:00 2026-06-08T03:08:12+00:00

I have the following problem (just learning OOP style C++): I have 3 classes:

  • 0

I have the following problem (just learning OOP style C++): I have 3 classes:

  1. app
  2. objectManager
  3. object

The app class sets up startup variables and creates an instance of objectManager. It tells objectManager to load values from different files and to precalculate some data in a vector what will be required for each object.

My problem is the following: I would like to use a precalculated vector accessable for each object. I don’t understand how can I possibly access this variable from the objects, as they know absolutely nothing about the manager’s instance.

I’ve read about singletons but I have no idea how should I implement it.

I’ve read about static data members but AFAIK it seems that I still have to somehow connect to the objectManager instance, so it doesn’t help me.

I’ve tried global variables (I know…), but ended up with compiling errors.

I’ve tried to put static variables outside of a class definition in objectManager.h (is it a global case?), and I ended up with something strange:
I can access the variables from all parts of the program what includes objectManager.h, but it’s value is different/uninitialized in different classes.

objectManager.h

#pragma once
#include "Object.h"

static int xxxtest;

class objectManager
{

objectManager.cpp

xxxtest = 123456;
cout << xxxtest << endl;
-> 123456

while in object.cpp or app.cpp (after)

cout << xxxtest << endl;
-> 0

Can you explain to me what is happening here?

Can you recommend me a simple and clean way of organizing such a program, while making a precalculated variable accessable to other parts of the program?

  • 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-08T03:08:13+00:00Added an answer on June 8, 2026 at 3:08 am

    Static class variables are in fact the solution.

    Static variables in classes need to be declared in the CPP file, right after the include.

    Header:

    class Class {
      static int staticVariable;
      static void staticFunction();
      //...
    };
    

    CPP:

    int Class::staticVariable = 5;
    

    Do that, and all Object instances have access to the static variable (if it’s private) and all members of the entire Program can access it (if it’s public) by stating:

    int Value = Class::staticVariable;
    

    I hope this helps shed some light.
    Ah yeah, I used the code from http://de.wikibooks.org/wiki/C++-Programmierung:_Statische_Elemente_in_Klassen (Page is in German) as Reference.

    To get back to your problem, that would mean:

    Header:

    class objectManager {
        static std::vector<double> Precompiled;
    };
    

    CPP:

    std::vector<double> Precompiled;    //Initialized automatically.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my PDO class. I just have started learning OOP
I'm just learning JavaScript Objects and have a question/problem. I have the following code
I am just starting out with JNI and I have a following problem. I
I am just starting working with jquery and have encountered the following problem <div
I am just learning MySQL and I have a problem. Sometimes for various queries
I'm just learning java and following a book. I have a program written via
I have a nested attributes form with the following and am just learning how
I'm just starting out with learning Java, and have run into a problem. When
I have just started learning the concept of Object oriented programming and have put
Just started learning Jquery - here's my problem: I have six divs on a

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.