In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to create a Visual C++ project". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to create a Visual C++ project.
It wraps pointers into classes and overloads the dereference operator operator * and member selection operator operator-> to mimic the behavior of pointers
For example, the following Visual C++ code
# include
< cstring ># include
< memory ># include
< iostream >Class string {public: string (const char* cstr) {_ data=new char [strlen (cstr) + 1]; strcpy (_ data, cstr);} ~ string () {delete [] _ data;} const char* c_str () const {return _ data;} private: char* _ data;}; void foo () {
Because str is a local object of the function, at the end of the lifetime of the function exit point, the destructor of auto_ptr automatically destroys the string object maintained by the internal pointer (previously allocated through the new expression in the constructor), and then executes the destructor of string to release the memory dynamically applied for the actual string. It is also possible to manage other types of resources in string, such as synchronized resources for multithreaded environments.
Auto_ptr
< string >Str1 (new string (
< str1 >)); cout c_str (); auto_ptr
< string >Str2 (str1); / / str1 internal pointer no longer points to the original object cout c_str (); cout c_str (); / / undefined, str1 internal pointer is no longer valid
Now we have the simplest garbage collection mechanism (I conceal that in string, you still need to code yourself to control the dynamic creation and destruction of objects, but the rule in this case is extremely simple: allocate resources in the constructor and release resources in the destructor, as if the pilot had to check the landing gear after take-off and before landing.) Even if an exception occurs in the foo function, the lifetime of str ends, and C++ guarantees that everything that happens when you exit naturally will be as valid when the exception occurs.
Auto_ptr is just one kind of smart pointers, and its replication behavior provides the semantics of ownership transfer, that is, smart pointers transfer ownership of internally maintained actual pointers when copying, for example:
Template
< typename T >Class shared_ptr {private: class implement / / implementation class, reference count {public: implement (T* pp): P (pp), refs (1) {} ~ implement () {delete p;} T* p; / / actual pointer size_t refs; / / reference count}; implement* _ impl Public: explicit shared_ptr (T* p): _ impl (new implement (p)) {} ~ shared_ptr () {decrease (); / / count decreasing} shared_ptr (const shared_ptr& rhs): _ impl (rhs._impl) {increase (); / / count increment}
Sometimes, when you need to share the same object, auto_ptr is not enough. For some historical reasons, Visual C++ 's standard library does not provide other forms of smart pointers, so there is no way out? In the main () function, you first call foo1 (val), which uses a local object, temp, which shares the same data as val and modifies the actual value.
After the function returns, the value owned by val also changes, when in fact val itself has not been modified. Then you call foo2 (val), which uses an unnamed temporary object to create a new value, modifies the val with an assignment expression, and the val and the temporary object have the same value, and when the function returns, val still has the correct value.
At this point, I believe you have a deeper understanding of "how to create a Visual C++ project". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.