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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:52:39+00:00 2026-05-16T17:52:39+00:00

I’m trying to create a Breakout clone using C++, and so have several objects

  • 0

I’m trying to create a Breakout clone using C++, and so have several objects (like ball, paddle, powerupicon, block, etc). I understand that it’s bad practice to have them at global scope, so they’re initialized inside main(). The problem comes in with needing to do stuff with those objects from inside other functions (like redraw(), or reset_game()). The only method I can think of to do this would be to pass references of the objects into each function, but I’m not sure if this is the best approach.

Basically, what’s the best way to do stuff with these objects from within a function? Pass them by reference? Make them global but within a namespace? Or something completely different?

  • 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-16T17:52:40+00:00Added an answer on May 16, 2026 at 5:52 pm

    A simple (not necessarily flexible or powerful) approach is to define a base game_object class that defines your interface with game objects, and store those. Your objects inherit from it.:

    class game_object
    {
    public:
        virtual ~game_object() {}
    
        virtual void update() = 0;
        virtual void draw() = 0;
    
        // get and set position
        virtual vector3 position() = 0;
        virtual void position(const vector3&) = 0;
    
        // etc...
    };
    
    class ball : public game_object
    {
    public:
        void update() { /* update the ball */ }
        void draw() { /* draw the ball */ }
    
        // etc
    };
    
    // etc
    

    Now you have a common way of using a game object. Next, you can store these in a map (preferably an unordered_map if you have Boost, TR1, or C++0x), and make that map globally available:

    // game_object_manager.h
    typedef std::map<std::string, game_object*> game_object_manager;
    
    extern game_object_manager manager;
    

    You define this in a translation unit (either main.cpp or a game_object_manager.cpp) somewhere. Lastly, you use it by inserting things by name:

    // somewhere, add the ball
    manager.insert(std::make_pair("ball", new ball()));
    

    And use it:

    game_object* b = manager["ball"];
    if (!b) /* there is no ball object */
    
    // use b as ball */
    

    Again, this is a simple solution and isn’t necessarily robust, a best practice, or flexible. But for a first game, it’ll work fine. (To improve it, you want to look into exception safety, smart pointers, and other things (game books). I recommend you do this before you try to make games.)

    • 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
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.