Sams Teach Yourself C++ in One Hour a Day, Seventh Edition by Siddhartha Rao
Author:Siddhartha Rao
Language: eng
Format: epub
Publisher: Sams Publishing
Published: 2012-04-28T16:00:00+00:00
* * *
Caution
As far as possible, you should refrain from using reinterpret_cast in your applications because it allows you to instruct the compiler to treat type X as an unrelated type Y, which does not look like good design or implementation.
* * *
Using const_cast
const_cast enables you to turn off the const access modifier to an object. If you are wondering why this cast is necessary at all, you are probably right in doing so. In an ideal situation where programmers write their classes correctly, they remember to use the const keyword frequently and in the right places. The practical world is unfortunately way too different, and code like following is quite prevalent:
Click here to view code image
class SomeClass
{
public:
// ...
void DisplayMembers (); // a display function ought to be const
};
So, when you program a function such as
Click here to view code image
void DisplayAllData (const SomeClass& mData)
{
mData.DisplayMembers (); // Compile failure
// reason for failure: call to a non-const member using a const reference
}
You are evidently correct in passing the mData object as a const reference. After all, a display function should be read-only and should not be allowed to call non-const member functions—that is, should not be allowed to call a function that can change the state of the object. However, the implementation of DisplayMembers(), which also ought to be const, unfortunately is not. Now, so long as SomeClass belongs to you and the source code is in your control, you can make corrective changes to DisplayMembers(). In many cases, however, it might belong to a third-party library, and making changes to it is not possible. In situations such as these, const_cast is your savior.
The syntax for invoking DisplayMembers() in such a scenario is
Click here to view code image
void DisplayAllData (const SomeClass& mData)
{
SomeClass& refData = const_cast <SomeClass&>(mData);
refData.DisplayMembers(); // Allowed!
}
Note that using const_cast to invoke non-const functions should be a last resort. In general, keep in mind that using const_cast to modify a const object can result in undefined behavior.
Note also that const_cast can be used with pointers:
Click here to view code image
void DisplayAllData (const SomeClass* pData)
{
// pData->DisplayMembers(); Error: attempt to invoke a non-const function!
SomeClass* pCastedData = const_cast <SomeClass*>(pData);
pCastedData->DisplayMembers(); // Allowed!
}
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Whiskies (Collins Gem) by dominic roskrow(45074)
Spell It Out by David Crystal(36041)
Cecilia; Or, Memoirs of an Heiress — Volume 1 by Fanny Burney(32434)
Cecilia; Or, Memoirs of an Heiress — Volume 2 by Fanny Burney(31871)
Cecilia; Or, Memoirs of an Heiress — Volume 3 by Fanny Burney(31854)
Beautiful Disaster by McGuire Jamie(25252)
Trainspotting by Irvine Welsh(21519)
Chic & Unique Celebration Cakes by Zoe Clark(19960)
The Secret History by Donna Tartt(18846)
How High Can a Kangaroo Hop? by Jackie French(18771)
Twilight of the Idols With the Antichrist and Ecce Homo by Friedrich Nietzsche(18503)
All the Missing Girls by Megan Miranda(15572)
Cat's cradle by Kurt Vonnegut(15184)
Ready Player One by Cline Ernest(14524)
Fifty Shades Freed by E L James(13157)
For the Love of Europe by Rick Steves(12998)
4 3 2 1: A Novel by Paul Auster(12283)
Crooked Kingdom: Book 2 (Six of Crows) by Bardugo Leigh(12218)
Grundlagen Kreatives Schreiben (German Edition) by Helfferich Pia(10406)