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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:19:32+00:00 2026-05-13T21:19:32+00:00

I am moving to C++ from Java and I am having a lot of

  • 0

I am moving to C++ from Java and I am having a lot of trouble understanding the basics of how C++ classes work and best practices for designing them. Specifically I am wondering if I should be using a pointer to my class member in the following case.

I have a custom class Foo which which represents the state of a game on a specific turn, and Foo has a member variable of custom class Bar which represents a logical subset of that game state. For example Foo represents a chess board and Bar represents pieces under attack and their escape moves (not my specific case, but a more universal analogy I think).

I would like to search a sequence of moves by copying Foo and updating the state of the copy accordingly. When I am finished searching that move sequence I will discard that copy and still have my original Foo representing the current game state.

In Foo.h I declare my Foo class and I declare a member variable for it of type Bar:

class Foo {
    Bar b;
public:
    Foo();
    Foo(const Foo& f);
}

But in the implementation of my Foo constructors I am calling the Bar constructor with some arguments specific to the current state which I will know at run time. As far as I understand, this means that a Bar constructor is called twice – once because I wrote “Bar b;” above (which calls the default constructor if I understand correctly), and once because I am writing something like “b = Bar(arg1,arg2,…)” in Foo::Foo() and Foo::Foo(const Foo& f).

If I am trying to make as many copies of Foo per second as possible, this is a problem, right?

I am thinking a simple solution is to declare a pointer to a Bar instead: “Bar *b”, which should avoid instantiating b twice. Is this good practice? Does this present any pitfalls I should know about? Is there a better solution? I can’t find a specific example to help me (besides lots of warnings against using pointers), and all the information about designing classes is really overwhelming, so any guidance would be greatly appreciated!

EDIT: Yes, I will have all the information necessary to create Bar when I create my Foo. I think everyone inferred this, but to make it clear, I have something more like this already for my default constructor:

Foo(int k=5);

and in Foo.cpp:

Foo::Foo(int k) {
    b = Bar(k);
    ...
}

and then my Foo and its Bar member are updated incrementally as the game state changes.

So calling my custom Bar constructor in my Foo constructor declaration initialization list looks like the best way to do it. Thank you for the answers!

  • 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-13T21:19:33+00:00Added an answer on May 13, 2026 at 9:19 pm

    Ideally you’d have all the information necessary to setup Bar at the time Foo is constructed. The best solution would be something like:

    class Foo { 
        Bar b; 
    public: 
        Foo() : b() { ... }; 
        Foo(const Foo& f) : b(f.a, f.b) { ... }; 
    } 
    

    Read more about constructor initialization lists (which has no direct equivalent in Java.)

    Using a pointer pb = new Bar(arg1, arg2) will actually most likely deteriorate your performance since heap allocation (which could involve, among other things, locking) can easily become more expensive than assigning a non-default constructed Bar to a default-constructed Bar (depending, of course, by how complex your Bar::operator= is.)

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

Sidebar

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.