久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++:為什么 const_cast 是邪惡的?

C++: Why is const_cast evil?(C++:為什么 const_cast 是邪惡的?)
本文介紹了C++:為什么 const_cast 是邪惡的?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我一直聽到這種說法,雖然我實在找不到 const_cast 是邪惡的原因.

I keep hearing this statement, while I can't really find the reason why const_cast is evil.

在下面的例子中:

template <typename T>
void OscillatorToFieldTransformer<T>::setOscillator(const SysOscillatorBase<T> &src)
{
    oscillatorSrc = const_cast<SysOscillatorBase<T>*>(&src);
}

我正在使用引用,并且通過使用 const,我可以保護我的引用不被更改.另一方面,如果我不使用 const_cast,代碼將無法編譯.為什么 const_cast 在這里會很糟糕?

I'm using a reference, and by using const, I'm protecting my reference from being changed. On the other hand, if I don't use const_cast, the code won't compile. Why would const_cast be bad here?

同樣適用于以下示例:

template <typename T>
void SysSystemBase<T>::addOscillator(const SysOscillatorBase<T> &src)
{
    bool alreadyThere = 0;
    for(unsigned long i = 0; i < oscillators.size(); i++)
    {
        if(&src == oscillators[i])
        {
            alreadyThere = 1;
            break;
        }
    }
    if(!alreadyThere)
    {
        oscillators.push_back(const_cast<SysOscillatorBase<T>*>(&src));
    }
}

請給我提供一些例子,我可以從中看出使用 const_cast 是個壞主意/不專業.

Please provide me some examples, in which I can see how it's a bad idea/unprofessional to use a const_cast.

感謝您的任何努力:)

推薦答案

因為你在阻撓 const 的目的,是為了防止你修改參數.因此,如果你拋棄了某些東西的 const 性質,它就會毫無意義并且會使你的代碼變得臃腫,它會讓你違背你對函數用戶做出的不會修改參數的承諾.

Because you're thwarting the purpose of const, which is to keep you from modifying the argument. So if you cast away the constness of something, it's pointless and bloating your code, and it lets you break promises that you made to the user of the function that you won't modify the argument.

此外,使用 const_cast 會導致未定義的行為.考慮這個代碼:

In addition, using const_cast can cause undefined behaviour. Consider this code:

SysOscillatorBase<int> src;
const SysOscillatorBase<int> src2;

...

aFieldTransformer.setOscillator(src);
aFieldTransformer.setOscillator(src2);

在第一次通話中,一切都很好.你可以拋棄一個不是真正 const 的對象的 constness 并對其進行很好的修改.然而,在第二次調用中,在 setOscillator 中,您正在拋棄真正的 const 對象的 constness.如果你碰巧在那里的任何地方修改了那個對象,你就會通過修改一個真正是 const 的對象來導致未定義行為.由于您無法判斷標記為 const 的對象是否真的 const 聲明它的位置,因此您永遠不應該使用 const_cast 除非你確定你永遠不會改變對象.如果你不這樣做,那又有什么意義呢?

In the first call, all is well. You can cast away the constness of an object that is not really const and modify it fine. However, in the second call, in setOscillator you are casting away the constness of a truly const object. If you ever happen to modify that object in there anywhere, you are causing undefined behaviour by modifying an object that really is const. Since you can't tell whether an object marked const is really const where it was declared, you should just never use const_cast unless you are sure you'll never ever mutate the object ever. And if you won't, what's the point?

在您的示例代碼中,您存儲了一個指向可能是 const 的對象的非 const 指針,這表明您打算改變該對象(否則為什么不只是存儲一個指向 const 的指針?).這可能會導致未定義的行為.

In your example code, you're storing a non-const pointer to an object that might be const, which indicates you intend to mutate the object (else why not just store a pointer to const?). That might cause undefined behaviour.

另外,這樣做可以讓人們向您的函數傳遞一個臨時變量:

Also, doing it that way lets people pass a temporary to your function:

blah.setOscillator(SysOscillatorBase<int>()); // compiles

然后您將存儲一個指向臨時對象的指針,當函數返回1時該指針將無效.如果您采用非const 引用,則不會遇到此問題.

And then you're storing a pointer to a temporary which will be invalid when the function returns1. You don't have this problem if you take a non-const reference.

另一方面,如果我不使用 const_cast,代碼將無法編譯.

On the other hand, if I don't use const_cast, the code won't compile.

然后更改您的代碼,不要添加演員表以使其工作.編譯器沒有編譯它是有原因的.現在您知道原因了,您可以讓 vector 指向 const 的指針,而不是將一個方孔變成圓形以適合您的掛鉤.

Then change your code, don't add a cast to make it work. The compiler is not compiling it for a reason. Now that you know the reasons, you can make your vector hold pointers to const instead of casting a square hole into a round one to fit your peg.

因此,在所有情況下,最好讓您的方法接受非 const 引用,而使用 const_cast 幾乎從來都不是一個好主意.

So, all around, it would be better to just have your method accept a non-const reference instead, and using const_cast is almost never a good idea.

1 實際上是在調用函數的表達式結束時.

1 Actually when the expression in which the function was called ends.

這篇關于C++:為什么 const_cast 是邪惡的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 精品自拍视频在线观看 | 69精品久久久久久 | 国产中文在线 | 日韩av在线中文字幕 | 最新国产精品精品视频 | 亚洲有码转帖 | 亚洲欧美视频一区二区 | 亚洲午夜精品一区二区三区 | 日韩欧美一区二区三区免费观看 | 丁香婷婷综合激情五月色 | 精品视频免费 | 中文字幕亚洲在线 | 伊人色综合久久天天五月婷 | 日本精品一区二区三区在线观看视频 | 一区二区三区成人 | 精品久久一区 | 麻豆视频在线免费观看 | 一区二区三区欧美在线观看 | 亚洲精品美女视频 | 81精品国产乱码久久久久久 | 91就要激情 | 亚洲国产aⅴ精品 | 欧美亚洲日本 | 91中文字幕在线观看 | 免费在线观看黄视频 | 国产精品视频一区二区三区 | 男女视频在线观看网站 | 国产精品欧美一区二区三区不卡 | 国产精品中文字幕在线 | 国产一区二区三区在线视频 | 91影片| av影音在线 | 国产精品视频导航 | 国产性色视频 | 久久久久久久夜 | av在线免费观看网站 | 真人毛片| 国产一级电影在线 | 色综合天天天天做夜夜夜夜做 | 色橹橹欧美在线观看视频高清 | 亚洲免费网址 |