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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:25:02+00:00 2026-06-06T06:25:02+00:00

I think I know C++ reasonably well and I am thinking about implementing something

  • 0

I think I know C++ reasonably well and I am thinking about implementing something a bit bigger than a “toy” program. I know the difference between stack- and heap-memory and the RAII-idiom.

Lets assume I have a simple class point:

class point {
public:
    int x;
    int y;
    point(int x, int y) : x(x), y(y) {}
};

I would allocate points always on the stack, since the objects are small. Since on 64-bit machines sizeof(point) == sizeof(void*), if a am not wrong, I would go even further and pass points by value by default.

Now lets assume a more complex class battlefield, that I want to use in the class game:

class battlefield {
public:
    battlefield(int w, int h, int start_x, int start_y, istream &in) {
        // Complex generation of a battlefield from a file/network stream/whatever.
    }
};

Since I really like RAII and the automatic cleanup when an object leaves the scope I am tempted to allocate the battlefield on the stack.

game::game(const settings &s) :
        battlefield(s.read("w"), s.read("h"), gen_random_int(), gen_random_int(), gen_istream(s.read("level_number"))) {
    // ...
}

But I have several problems now:

  • Since this class has not got a zero-args-constructor I have to initialize it in the initialisation list of the class I use battlefield in. This is cumbersome since I need a istream from somewhere. This leads to the next problem.

  • The complex constructors “snowball” at some point. When I use battlefield in the game class and initialize it in the initialisation list the constructor of game, the constructor of game will become fairly complex too and the initialisation of game itself might become cumbersome too. (When I decide to take the istream as argument of the game constructor)

  • I need auxiliary functions to fill in complex parameters.

I see two solutions to this problem:

  • Either I create a simple constructor for battlefield that does not initialize the object. But this approach has the problem that I have a half-initialized object, aka an object that violates the RAII-idiom. Strange things might happen when calling methods on such an object.

    game::game(const settings &s) {
        random_gen r;
        int x = r.random_int();
        int y = r.random_int();
        ifstream in(s.read("level_number"));
        in.open();
        this->battlefield.init(s.read("w"), s.read("h"), x, y, in);
        // ...
    } 
    
  • Or I allocate battlefield on the heap in the game constructor. But I have to beware of exceptions in the constructor and I have to take care that the destructor deletes the battlefield.

    game::game(const settings &s) {
        random_gen r;
        int x = r.random_int();
        int y = r.random_int();
        ifstream in(s.read("level_number"));
        in.open();
        this->battlefield = new battlefield(s.read("w"), s.read("h"), x, y, in);
        // ...
    } 
    

I hope you can see the problem I am thinking of. Some questions that arise for me are:

  • Is there a design pattern for this situations I do not know?

  • What is the best practise in bigger C++ projects? Which objects are allocated on the heap, which ones are allocated on the stack? Why?

  • What is the general advice regarding the complexity of constructors? Is reading from a file too much for a constructor? (Since this problem mostly arises from the complex constructor.)

  • 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-06T06:25:04+00:00Added an answer on June 6, 2026 at 6:25 am

    You could let your battlefield be constructed from settings:

    explicit battlefield(const settings& s);
    

    or alternatively, why not create a factory function for your battlefield?

    E.g.

    battlefield CreateBattlefield(const settings& s)
    {
        int w = s.read("w");
        int h = s.read("w");
        std::istream& in = s.genistream();
        return battlefield(w, h, gen_random_int(), gen_random_int(), in);
    }
    
    game::game(const settings &s) :
        battlefield(CreateBattlefield(s)) {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I think I know the answer to this, but I would appreciate anybody
for some reason (and I think I know why), when more than one row
I think know how to do this in C# but I'm having syntax trouble
I think I know what a B-tree is but what is a B-tree page?
I've been doing some research and I think I know the answer already, but
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
I think I already know the answer to this but thought I would ask
I think I probably know that the most common suggestion will be change the
I think i already know the answer to this, but i cannot find anything
I know Python (and a bunch of other languages) and I think it might

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.