Search code examples
c++performancepointersclass-design

C++: How to improve performance of custom class that will be copied often?


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!


Solution

  • 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.)