when use object replace smart pointer as member in class

such as the below code, EmbedderEngine has some members, what question I asked is when using a raw object as a member and when using a smart pointer as a member of a class. why not always use a smart pointer? can you help me?

1
2
3
4
5
6
7
8
9
10
11
class EmbedderEngine{
 .....
 private:
  const std::unique_ptr<EmbedderThreadHost> thread_host_;
  TaskRunners task_runners_;
  RunConfiguration run_configuration_;
  std::unique_ptr<ShellArgs> shell_args_;
  std::unique_ptr<Shell> shell_;
  std::unique_ptr<EmbedderExternalTextureResolver> external_texture_resolver_;
  FML_DISALLOW_COPY_AND_ASSIGN(EmbedderEngine);
};


> using a raw object as a member and when using a smart pointer as a member of a class.
> why not always use a smart pointer?

Prefer scoped objects, don’t heap-allocate unnecessarily
Reason A scoped object is a local object, a global object, or a member. This implies that there is no separate allocation and deallocation cost in excess of that already used for the containing scope or object. The members of a scoped object are themselves scoped and the scoped object’s constructor and destructor manage the members’ lifetimes.
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-scoped
Last edited on
Topic archived. No new replies allowed.