In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use factory functions in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
If you need "virtual behavior" during construction, use the factory function Reason (reason)
If the state of the base class object must depend on the derived part of the object, we need to use virtual functions (or equivalents) to minimize the time window in which the imperfectly constructed object is misused.
Note (Note)
The return type of the factory should usually return unique_ptr; by default. If some uses need to be shared, the caller can move the unique_ptr to shared_ptr. However, if the author of the factory knows that all the uses of the returned object are shared, he can also return shared_ptr so that make_shared can be used inside the function body to save a memory allocation.
Example, bad (negative example) class B {
Public:
B () {
/ *. /
F (); / / BAD: C.82: Don't call virtual functions in constructors and destructors
/ *. /
}
Virtual void f () = 0
}; Example (example) class B {
Protected:
Class Token {}
Public:
Explicit B (Token) {/ *... * /} / / create an imperfectly initialized object
Virtual void f () = 0
Template
Static shared_ptr create () / / interface for creating shared objects
{
Auto p = make_shared (typename T::Token {})
P-> post_initialize ()
Return p
}
Protected:
Virtual void post_initialize () / / called right after construction
{/ *... * / f (); / *... * /} / / GOOD: virtual dispatch is safe
}
Class D: public B {/ / some derived class
Protected:
Class Token {}
Public:
Explicit D (Token): B {B::Token {}}
Void f () override {/ *... * /}
Protected:
Template
Friend shared_ptr B::create ()
}
Shared_ptr p = D::create (); / / creating a D object
The constructor called by make_shared must be public. By requiring a protected token to ensure that the constructor cannot be called publicly, we avoid the outflow of partially constructed objects. Since a factory method create () is provided, the construction process (on free storage) can be easily implemented.
Note (Note)
The conventional factory method allocates object memory on free storage, not within a stack or closed object.
This is the end of the content of "how to use the factory function in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.