指针 转 智能指针_智能指针-它们真的那么聪明吗?
指針 轉 智能指針
Smart pointers - Are they really that smart?
智能指針-它們真的那么聰明嗎?
You might have heard something called smart pointers on your lectures, or that many game companies use them. The reason why just you, should use them might not be clear. Even if you would want to use them you may not know how you do that. The reason why you may not know that, is because the use of templates. Without templates, these smart pointers would actually be more accurate called "dumb pointers." We will cover "dumb pointers" later in our goal to achieve smart pointers.
您可能在您的演講中聽到了稱為智能指針的內容,或者許多游戲公司都在使用它們。 只是您應該使用它們的原因可能不清楚。 即使您想使用它們,也可能不知道該怎么做。 您可能不知道這一點的原因是因為使用了模板。 如果沒有模板,這些智能指針實際上將更準確地稱為“啞指針”。 我們稍后將在實現智能指針的目標中介紹“啞指針”。
What are smart pointers, and what are they good for?
什么是智能指針,它們有什么用?
Smart pointers is basicly your typical object. However, this object is built to be a container of sorts, a container that stores another object of chosen type. This container can then later call upon it's content and all it's member methods and attributes. That gives us the very specific advantage of calling upon the content object deconstructor, meaning removing it from memory automatically.
智能指針基本上是您的典型對象。 但是,此對象被構建為各種容器,該容器存儲了所選類型的另一個對象。 然后,此容器可以稍后調用其內容及其所有成員方法和屬性。 這給了我們調用內容對象解構函數的非常特殊的優勢,這意味著自動將其從內存中刪除。
The reason why this are good, is pretty straight forward. To decrease the number of bugs you probably will run into, if you would not to be using it.
之所以如此好,很簡單。 如果您不打算使用它,則可以減少可能遇到的錯誤的數量。
When you create a program, you do not want memory leakage or memory that takes room without reason. Ideally, you want to load your object into memory, and delete it as soon as it is not going to be used again.
創建程序時,您不希望出現內存泄漏或內存無故占用空間的情況。 理想情況下,您要將對象加載到內存中,并在不再使用該對象時立即將其刪除。
The most common error, that will happen in your program, will be that you allocate memory, and never release it. When you create a object in the memory (using pointers) you have to delete it. This is sometimes something many programmers forget, not because they are lazy or dumb, but because they say they will get to it later.
在程序中會發生的最常見錯誤是分配內存,而從不釋放內存。 當您在內存中創建對象(使用指針)時,必須將其刪除。 有時候,這是許多程序員忘記的事情,不是因為他們懶惰或愚蠢,而是因為他們說以后會使用。
They continue to say that as the code grows, and finally ends up being over 10 000 rows of code. Having bugs appearing with that much code, is to put it mildly, not fun.
他們繼續說,隨著代碼的增長,最終最終超過了10 000行代碼。 這么多的代碼會出現錯誤,這是溫和的,不是很有趣。
Here comes the smart pointers to rescue. They will automatically when your program shuts down, remove any content you have stored inside it, hence eliminating all sorts of memory leakage.
救援的精明指針到了。 它們會在您的程序關閉時自動刪除所有存儲在其中的內容,從而消除各種內存泄漏。
How do i get started creating my own class for smart pointers?
我如何開始為智能指針創建自己的類?
This will be divided into two steps, firstly for those of you who do not understand or have used templates in c++ before. This step will be called "dumb pointers." The second step, will be called smart pointers. If you feel that you have good enough knowledge about how to use and create templates, you can jump straight to that step.
這將分為兩個步驟,首先是針對那些以前不了解或使用過c ++模板的人。 此步驟稱為“啞指針”。 第二步,稱為智能指針。 如果您認為自己具有足夠的有關如何使用和創建模板的知識,則可以直接跳到該步驟。
Step 1 - Making dumb pointers
第1步-制作啞指針
Imagine the scenario where we have the following class:
想象一下我們有以下課程的場景:
//Player.h - Header file #pragma once #include <iostream>class Player { public:Player(void);void SayHi();~Player(void); };//Player.cpp - Source file
//Player.cpp-源文件
#include "Player.h"Player::Player(void) { }void Player::SayHi() {std::cout << "Derp derp" << std::endl; }Player::~Player(void) {std::cout << "Master Removed me" << std::endl; }and then we have the main.cpp with the following code:
然后我們的main.cpp帶有以下代碼:
#include <iostream> #include "auto_ptr.h" #include "Player.h"using namespace std;int main() {Player *dumbpointer = new Player();dumbpointer->SayHi();//deallocate memory, and set the pointer to null, thanks Saradelete dumbpointer;dumbpointer = NULL;system("pause");return 0; }What we do here, is to allocate memory dynamicly, by using directive "new". The memory address to this allocation will be stored in our pointer, named "dumbpointer" of our class Player. Now dumbpointer simply points on the dynamicly allocated object, and we can use the operator -> to gain access to it from our code.
我們在這里所做的是通過使用指令“ new”動態分配內存。 此分配的內存地址將存儲在我們的指針中,該指針稱為類Player的“ dumbpointer”。 現在,dumbpointer只需指向動態分配的對象,我們就可以使用運算符->從我們的代碼中訪問它。
When we write dumbpointer->SayHi(); the code will look what the dumbpointer points to, and go there to find the function we called. Later we have to call delete dumbpointer, to make sure no memoery leak occurs. That's all it does. It only points. What if pointers could do more? Like noticing when the program exits, and no longer point.
當我們編寫dumbpointer-> SayHi();時 該代碼將查找啞指針指向的內容,然后去找到我們調用的函數。 稍后,我們必須調用delete dumbpointer,以確保不會發生備忘錄泄漏。 這就是全部。 它只是點。 如果指針可以做更多呢? 就像在程序退出時注意,不再指向。
Imagine someone in real life pointing at a house that no longer exists. That would be kind of weird huh? This is when smart pointers comes to the rescue!
想象一下現實生活中有人指向一所不再存在的房屋。 那會有點奇怪吧? 這是聰明的指針來救援的時候!
Step 2 - Making smart pointers
第2步-制作智能指針
When making smart pointers, you have to utilize templates. The reason why, is because you do not know what type of object you want your smart pointer to contain. Up until your first time using templates you always had to declare what type of object you want to use or create. Like a string, int or bool. Template holds whatever type we want, without us having to declare overloads or several attributes that holds that type. You can think that templates are holding types, that are chosen when you compile your code.
制作智能指針時,必須使用模板。 之所以這樣,是因為您不知道智能指針要包含哪種類型的對象。 直到您第一次使用模板,您始終必須聲明要使用或創建哪種類型的對象。 像字符串一樣,int或bool。 模板可以保存我們想要的任何類型,而不必聲明重載或保留??該類型的多個屬性。 您可以認為模板是保存類型,這些類型是在編譯代碼時選擇的。
How can I make this smart pointer with template class? Here we only need one thing, a header file called auto_ptr.h
如何使用模板類制作此智能指針? 在這里,我們只需要一件事,即名為auto_ptr.h的頭文件
//Tell the compiler that we are making a template class, that can support all kind of types. //The type we later choose, will be stored in the identifer T. template <class T> class auto_ptr { public://explicit = Disable the use of auto_ptr<type> obj = something;//Constructorexplicit auto_ptr(T* pointer) : ptr(pointer){};//Deconstructor, will automaticly remove the object from memory when we reach eop (end of program)~auto_ptr(){delete ptr;}//Link so that when a pointer is used with our auto_ptr, it will be linked to the contained object inside our auto_ptr instead.T& operator*(){return *ptr;}//When we use the operator -> we want it to return the contained object inside auto_ptr too.T* operator->(){return ptr;} private://Our member attribute, which stores the object we want contained in our auto_ptrT* ptr; };When we later implement this class in our main.cpp we do like this:
當我們稍后在main.cpp中實現此類時,我們這樣做:
#include <iostream> #include "auto_ptr.h" #include "Player.h"using namespace std;int main() {auto_ptr<Player> smartplayer(new Player);smartplayer->SayHi();system("pause");return 0; }What we do here, is to create our smart pointer. By writing <Player>, we tell our template class that T should store the type Player, which is the class we created. This means we can now look at our auto_ptr class, and imagine that instead of writing T in front of our operator overloads, we write Player. This should make everything much more clearer to you. Yes the result from our function will now give back a response that is of the type Player.
我們在這里要做的是創建我們的智能指針。 通過編寫<Player>,我們告訴模板類T應該存儲Player類型,這是我們創建的類。 這意味著我們現在可以看一下我們的auto_ptr類,并且可以想象,我們不是在運算符重載之前編寫T,而是編寫Player。 這應該使一切更加清晰。 是的,我們函數的結果現在將返回播放器類型的響應。
Except for how you create this smart pointer, its pretty similar to how you created the dumb pointer. When you call the identifier later, it looks the same as if it would be the dumb pointer. However, as soon as the program closes, the auto_ptr deconstructor will be called and directly remove our Player allocation from memory. This is something the dumb pointer will not, unless we specify it to do so with the directive delete.
除了如何創建此智能指針之外,它與創建啞指針的方式非常相似。 以后調用標識符時,它看起來就像是啞指針。 但是,一旦程序關閉,就會調用auto_ptr解構函數,并直接從內存中刪除Player分配。 除非我們使用指令delete進行指定,否則這將是啞指針無法做到的。
Step 3 - Smart pointers and praxis
第3步-智能指針和實踐
The above text should make you see the advantages of using smart pointers. However this class is not something you should use in your application. There are already several solutions of smart_pointers out there that will support many many more different solutions and work much better than the above smart pointer class does.
上面的文字應該使您看到使用智能指針的優點。 但是,此類不是您應在應用程序中使用的類。 已經有幾種smart_pointers解決方案,它們將支持更多不同的解決方案,并且比上述智能指針類的效果更好。
The new std has a set of these smart pointers. To name a few: std::unique_ptr, std::shared_ptr and std::weak_ptr.
新的std具有一組這些智能指針。 僅舉幾例:std :: unique_ptr,std :: shared_ptr和std :: weak_ptr。
Although, a open source library that exists today called boost, may be more to your liking.
雖然,如今存在一個稱為boost的開源庫,但您可能更喜歡。
The boost library has several advantages, and is a library that is used in for instance CryEngine 1,2 and 3.
boost庫具有多個優點,并且是一個庫,例如在CryEngine 1,2和3中使用。
You can find the boost library here: http://www.boost.org/
您可以在以下位置找到增強庫: http : //www.boost.org/
翻譯自: https://www.experts-exchange.com/articles/10083/Smart-pointers-Are-they-really-that-smart.html
指針 轉 智能指針
總結
以上是生活随笔為你收集整理的指针 转 智能指针_智能指针-它们真的那么聪明吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高性能RPC框架gRPC竟恐怖如斯~
- 下一篇: crh寄存器_端口配置寄存器CRH怎么弄