0

I am learning C++ OO design. When to declare class member on heap vs stack?

class RacingLiveEvent
{
public:
    playerlist  *pl;
}

vs

class RacingLiveEvent
{
public:
    playerlist  pl;
}

What design/runtime/compiletime considerations will help me decide. Any suggestions please?

I see with heap, I can define each class in its own header file. If I use static object, I need all the dependent class to be in one file.

Edit: This question is more about class design than difference between pointer and reference as pointed out by some moderator/stackoverflow AI. We already have some answers from Peter and Sombrero in comments.

Anup Buchke
  • 4,300
  • 5
  • 19
  • 34
  • Possible duplicate of [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – Fureeish Jul 27 '19 at 23:33
  • @Fureeish But OP doesn't ask about references? – HolyBlackCat Jul 27 '19 at 23:34
  • "*If I use static object, I need all the dependent class to be in one file.*" Well, that's what `#include` is for. – Yksisarvinen Jul 27 '19 at 23:35
  • @HolyBlackCat but doesn't the answers to that question specifically go in detal why one would use a pointer variable altogether with heap allocation? – Fureeish Jul 27 '19 at 23:35
  • 2
    Always go for an object unless you somehow have to actually point to another object and have that object outlive your object's scope. – Hatted Rooster Jul 27 '19 at 23:35
  • 1
    I never think in terms of heap and stack, since they are irrelevant distinctions in C++ (specific to some platforms and implementations). I think in terms of whether an object needs to be dynamically allocated or not. Questions include: Is the number of objects needed fixed, or does it depend on data obtained at run time? Does the number of objects needed change over time as the program is run? Is the life-cycle of the object (e.g. when it is created and destroyed) different from that of the parent object or containing scope? – Peter Jul 27 '19 at 23:44
  • @SombreroChicken and Peter. Thanks for you suggestions. That helps. – Anup Buchke Jul 27 '19 at 23:46
  • 1
    My general rule of thumb is make it a member until you have a reason not to. Then make it a smart pointer (preferably unique_ptr). Tangential, but related... if the project is of moderate size or larger, consider using the "pimpl" pattern (pointer to implementation pattern) to segregate out outward facing API from inner workings; albeit at the cost of more maintenance effort and extra level of annoying indirection while debugging. – Eljay Jul 28 '19 at 01:23
  • Your title exhibits incorrect thinking. A class member is only on the stack if the member isn't a pointer *and* the class instance itself is on the stack. Most of the time an instance is on the heap, and so therefore are its members however declared. – user207421 Jul 28 '19 at 01:24
  • @ArneJ Method-local allocation is not static. Don't add to the confusion. – user207421 Jul 28 '19 at 11:59
  • Possible duplicate of [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – L. F. Jul 29 '19 at 01:37

1 Answers1

1

If you need to share/pass the member to other functions/classes use a pointer (better shared_ptr) given that the member is big such that copy is costly.

Pointer allows the member to outlive its scope, and you want to allow the member live longer than its scope if it is in use within some other classes or function when your object destructed.

A good use case for shared_ptr is some different type of objects sharing the same member:

class A{
     public:
     A(std::shared_ptr<SomeType> member) :
     member(member){}
     std::shared_ptr<SomeType> member;
     //
};

class B{
     public:
     B(std::shared_ptr<SomeType> member) :
     member(member){}
     std::shared_ptr<SomeType> member;
     //
}; 

Here A and B can share the same member and if they share, member dies only when everyone who are using member go out of their scope.

Please note that if you share your member with others using a bare pointer is not a good idea.

Other than this use stack.

Oblivion
  • 6,320
  • 2
  • 9
  • 30