|
-
March 24th, 2003, 03:11 AM
#1
Query on Static Cast
Hi folks,
I got a simple doubt. What are the pros and cons of using c style casting in c++ and vc++. I need to know the difference between
ptr = (class*) (expression) and
ptr = static_cast<class*> (expression).
At what risk could I use ptr = (class*) (expression) in c++ or vc++.
ciao
Life is short, enjoy it with a smile as long as it lasts.
User error. Replace User and press any key when ready. - Anonymous
-
March 24th, 2003, 03:56 AM
#2
Casting usually is considered as bad software design. I would not go that far (at least in Windows you are forced to do it by passing values to API functions (although you will not see it most of the time since it is done implicitely...)). Nevertheless if you need to cast use the casts which were introduced with C++. The following is a small description about C-style and C++-casts...
Casting means you change the representation of a variable by changing its type to a different one. In order to type-cast a simple object to another you use the traditional type casting operator. For example, to cast a floating point number of type 'double' to an integer of type 'int':
Code:
int i;
double d;
i = (int) d;
or also
This is quite good for basic types that have standard defined conversions, however this operators can also been indiscriminately applied on classes and pointers to classes. ANSI-C++ standard has defined four new casting operators: 'reinterpret_cast', 'static_cast', 'dynamic_cast' and 'const_cast' in order to control these types of conversions between classes...
Code:
reinterpret_cast<new_type>(expression)
dynamic_cast<new_type>(expression)
static_cast<new_type>(expression)
const_cast<new_type>(expression)
reinterpret_cast
'reinterpret_cast' casts a pointer to any other type of pointer. It also allows casting from pointer to an integer type and vice versa.
This operator can cast pointers between non-related classed. The operation results is a simple binary copy of the value from a pointer to the other. The content pointed does not pass any kind of check nor transformation between types.
In the case that the copy is performed from a pointer to an integer, the interpretation of its content is system dependent and therefore any implementation is non portable. A pointer casted to an integer enough large to fully contain it can be casted back to a valid pointer.
Code:
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B *>(a);
'reinterpret_cast' treats all pointers exactly as traditional type-casting operators do.
static_cast
'static_cast' allows to perform any casting that can be implicitly performed as well as also the inverse cast (even if this is not allowed implicitly).
Applied to pointers to classes, that is to say that it allows to cast a pointer of a derived class to its base class (this is a valid conversion that can be implicitly performed) and can also perform the inverse: cast a base class to its derivated class.
In this last case the base class that is being casted is not checked to determine wether this is a complete class of the destination type or not.
Code:
class Base {};
class Derived : public Base {};
Base *a = new Base;
Derived *b = static_cast<Derived *>(a);
'static_cast', aside from manipulating pointers to classes, can also be used to perform conversions explicitly defined in classes, as well as to perform standard conversions between fundamental types:
Code:
double d = 3.14159265;
int i = static_cast<int>(d);
dynamic_cast
'dynamic_cast' is exclusively used with pointers and references to objects. It allows any type-casting that can be implicitly performed as well as the inverse one when used with polymorphic classes, however, unlike static_cast, dynamic_cast checks, in this last case, if the operation is valid. That is to say, it checks if the casting is going to return a valid complete object of the requested type.
Checking is performed during run-time execution. If the pointer being casted is not a pointer to a valid complete object of the requested type, the value returned is a 'NULL' pointer.
Code:
class Base { virtual dummy() {} };
class Derived : public Base {};
Base* b1 = new Derived;
Base* b2 = new Base;
Derived* d1 = dynamic_cast<Derived *>(b1); // succeeds
Derived* d2 = dynamic_cast<Derived *>(b2); // fails: returns 'NULL'
If the type-casting is performed to a reference type and this casting is not possible an exception of type 'bad_cast' is thrown:
Code:
class Base { virtual dummy() {} };
class Derived : public Base { };
Base* b1 = new Derived;
Base* b2 = new Base;
Derived d1 = dynamic_cast<Derived &*>(b1); // succeeds
Derived d2 = dynamic_cast<Derived &*>(b2); // fails: exception thrown
const_cast
This type of casting manipulates the const attribute of the passed object, either to be set or removed:
Code:
class C {};
const C *a = new C;
C *b = const_cast<C *>(a);
Neither of the other three new cast operators can modify the constness of an object.
-
March 24th, 2003, 04:19 AM
#3
One slight addition to Andreas's excellent answer: const_cast can also cast the "volatile" qualifier in addition to the "const" qualifier. (It ought to be called cv_cast, I suppose).
Also one other thought on C-style versus C++-style: as Andreas mentioned, excessive casting probably indicates a fundamental flaw in your code. When that flaw is fixed, and you're searching your code for all the casts that can now be removed, think on which style you'd rather look for....
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 24th, 2003, 05:06 AM
#4
Thanks mate
Hi mate,
Thank you both for the excellent information. I had actually asked this question in the following scenario;
My code is written in VC++. Fo using GetDlgItem, I had used (Class*) trype of static cast. One of my team mates came back to me saying that I should be using static_cast<class*>(expression) instead of (class*) type of static cast as the latter is of c style casting. So I wanted to know that is there any specific advantages or disadvantages of using static_cast<class*> (expression) over (class*).
ciao
Life is short, enjoy it with a smile as long as it lasts.
User error. Replace User and press any key when ready. - Anonymous
-
March 24th, 2003, 06:51 AM
#5
To sum up: there are two main reasons to prefer C++-style casts over C-style
1) C++ casts are safer, since you need to use the correct one. static_cast, for example, won't accidentally cast away constness; dynamic_cast returns a null pointer if the cast is not legal. C-style casts just go ahead and screw up your program.
2) C++-style casts are easier to find with a search in the editor.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 24th, 2003, 08:46 AM
#6
Thank you mate
Thank you one and all mate.
Life is short, enjoy it with a smile as long as it lasts.
User error. Replace User and press any key when ready. - Anonymous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|