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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:36:41+00:00 2026-05-24T05:36:41+00:00

I have few files: main.cpp: int main{ … while(1){ … draw(); … } …

  • 0

I have few files:

main.cpp:
int main{
...
while(1){
...
draw();
...
}
...
return 0;
}

and draw.cpp:
I want to see objects and all manipulations here.
I cant make objects local to draw(), because draw() is inside loop,
so I will get many object constructor/destructor calls – so they are global. Also
I ve made init block to prevent unnecessary calls/assignments

draw.cpp:
Object A, B;
int initialized = 0;
void draw(){
if(!initialized){
    A.initialization;
    B.initialization;
    initialized = 1;
}
A.move(1,1);
B.rotate(45);
}

It works, but Im looking for better way to organize my code

Added:
Thanks for answers, looks like I have to read something about pattern designs

  • 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-05-24T05:36:42+00:00Added an answer on May 24, 2026 at 5:36 am

    Option 1

    Define a new Class called Draw and put the attributes into it. You need to modify main and draw files for this. With this you can avoid declaring anything global

    Option 2

    Define a class within draw.cpp called draw and add your current global variables as static member variables. Initialize and use them using static functions. With this you dont have to change main.
    This design technique is called Singleton (one of the design patterns)

    Example code
    draw.cpp

    class Draw
    {
    public:
      object A, B;
    
      static void init()
      {
        // init A
        // init B
        isInitialized = 1;
      }
    
      static int isInitialized;
    
      static Object & getA()
      {
         if(isInitialized == 0)
         {
           init();
         } 
         return A;
      }
      // similarly B
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In almost all of my projects I have few files that are project wide
I have a few internal Canvas xaml files which I want to load dynamically
I have a few .rb files and I want to use the same variables
I have a few files that were part of an update that were mistakenly
I have a few batch files I need to run frequently in developing a
I have a few text files and I'd like to count how many times
I have a few log files like these: /var/log/pureftpd.log /var/log/pureftpd.log-20100328 /var/log/pureftpd.log-20100322 Is it possible
I have a shell script which copies a few files to the current directory,
I have a few directories and sub-directories containing files with no file extension. I
I have a WiX project that installs a few EXE files. One is the

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.