Home > database >  C Sharing member variables to member instances of classes during initialization
C Sharing member variables to member instances of classes during initialization

Time:12-09

Currently using C 20.

I'm trying to share data from an instance of class A to its member instances of B and C during initialization. Rather than using getter/setters, singletons, dependency injections, etc, I was thinking of just sharing the data by passing them as arguments in the constructors of the member class variables, where each member instance would hold their share of data via some mean (e.g. reference, raw pointer, shared pointer, void pointer, etc).

Classes B and C are intended to always be initialized as members of class A. Ultimately, A should be the last 'owner' of the data such that when A gets deallocated during destruction, the shared data should be deallocated with it. With that in mind, which of these approaches would be acceptable; not violating any major rules and ensuring encapsulation?

  1. A stores and passes the data as shared pointers. B & C stores the arguments as shared pointers.

  2. A stores the data as normal variables, and passes them as void pointers. B & C stores the arguments as void pointers.

  3. A stores the data as normal variables, and passes them as references. B & C stores the arguments as references.

  4. A stores the data as normal variables, and passes them as raw pointers. B & C stores the arguments as raw pointers.

Pseudocode:

class B
{
private:
  // Some variable that holds the shared data
};

class C
{
private:
  // Some variable that holds the shared data
};

class A
{
  private:
    // Data to be shared to
    B b;
    C c;

    // Data to be shared
    SomeDataType first;
    SomeDataType second;

  public:
    A()
      : b{first, second}, c{first}
    {

    }
};

CodePudding user response:

A stores and passes the data as shared pointers. B & C stores the arguments as shared pointers.

No. A owns the data, no ownership is shared. ("[...], A should be the last 'owner' of the data such that when `A? gets deallocated during destruction, the shared data should be deallocated with it.", if it is the last and first then it can be the only one, no need to share.)

A stores the data as normal variables, and passes them as void pointers. B & C stores the arguments as void pointers.

No. void* is not a solution to a problem you do not have. When in the past one had to use void* there are better alternatives now. Though as there is no need here in the first place this is beyond the scope of this answer.

A stores the data as normal variables, and passes them as references. B & C stores the arguments as references.

No, maybe yes. Reference members have implications. The compiler cannot generate copying for you.

A stores the data as normal variables, and passes them as raw pointers. B & C stores the arguments as raw pointers.

That sounds reasonable. It is raw owning pointers that should be avoided. A raw non-owning pointer that never gets invalid (because the owner is destroyed after observers) is fine. The only phases during which you need to be careful not a not yet valid or not anymore valid pointer is during construction and destruction. It is worth mentioning that during construction of A it is ok to take pointers to members, but only after the member has been initialized you may safely dereference the pointer.

  •  Tags:  
  • c
  • Related