How should I create C ++ members that depend on each other?

I have a class using a library for communication that looks like this:

class Topic { Topic( Type T, String name ); }; class Reader { Reader (Topic, String name); }; class Writer { Writer (Topic, String name); }; 

I want to make the speaker class as follows:

 Talker (Type T, String name); 

And generate a Writer and Reader member using the constructor.

I tear between them using pointers:

 class Talker { Topic* m_Topic; Reader* m_Reader; Writer* m_Writer; Talker (Type T, String name) { m_Topic = new Topic (T, generateTopicName(name)); m_Reader = new Reader (m_Topic, generateReaderName(name)); m_Writer = new Writer (m_Topic, generateWriterName(name)); } }; 

Straight straight:

 class Talker { Topic m_Topic; Reader m_Reader; Writer m_Writer; Talker (Type T, String name) : m_Topic(T, name), m_Reader(m_Topic, generateReaderName(name)), m_Writer(m_Topic, generateWriterName(name)) {} }; 

I spoke with a colleague, and, apparently, the latter is bad due to the dependence on the member initialization order. However, it also has a working auto-copy constructor.

What is the best way to do something like this, especially if the list of member objects is increasing?

+4
source share
3 answers

This decision should not be based on the arguments your colleague proposes, as this is an invalid argument. You can control the initialization order even with objects - see My last paragraph. The decision should be based on:

1) Functional . Do you need polymorphic types? Will Topic , Reader and Writer be inherited? If so, you should use pointers to prevent objects from overlapping.

2) Logical Is Talker true owner of members (objects) or just points to some objects that are shared by several classes?

Old answer

An alternative is to use smart pointers instead of members. Thus, you still have the benefits of automatic memory management.

However, your colleague's argument is invalid and the latter option is not bad if you know your C ++. Elements are initialized in the order in which they are declared in the class . So m_Topic will be initialized first, m_Reader second and finally m_Writer . If the initialization order is important (code smell), simply reorder the members in the class definition.

+5
source

It depends on how you use these classes, but in general, the less heap allocations, the better, so I'm leaning towards the constructor with all these new . Is dependency on member initialization really a problem? I can imagine why this should be.

If you use pointers, it would be wise to do this with smart ones, std::unique_ptr is a canonical choice.

0
source

It really depends on your requirements.

pointer version is more flexible

  • Objects may be empty
  • Objects can be exchanged, cleaned

but

  • you need to take care of the correct destruction (well, smart pointers will help)
  • you need more memory at least 3 * 4 bytes in an environment with 32-bit memory, perhaps more because of the details of the memory management implementation, which is likely to double the numbers. It hurts when you have a lot of small objects.

The pointer model is faster

  • car and wheel situation (replacement)

Participant model is more likely

  • situation in the house and room (it was not possible to rent a room without changing the house).
0
source

Source: https://habr.com/ru/post/1403520/


All Articles