-
February 27th, 2010, 02:16 AM
#1
Static casting in C++
Hi,
I am looking for the answers for the following questions, please:
What will return if the static cast fails for pointers with polymorphic object?
What will return if the static cast fails for reference with polymorphic object?
What will return if the static cast fails for pointers with non-polymorphic object?
What will return if the static cast fails for reference with non-polymorphic object?
Can you do static casting with structures in the polymorphic object?
Can you do static casting with structures in the non-polymorphic object?
Can you do static casting with unions in the polymorphic object?
Can you do static casting with unions in the non polymorphic object?
Can you do static casting with virtual base class in the polymorphic object?
Can you do static casting with virtual base class in the non polymorphic object?
Can you do static casting with multiple inheritance in the polymorphic object?
Can you do static casting with multiple inheritance in the non polymorphic object?
Can you do static casting with enum type?
Can you do static casting with array type?
Thank You.
-
February 27th, 2010, 02:24 AM
#2
Re: Static casting in C++
You might want to elaborate on your questions with code, and at least try to compile some of them to see if you can answer your own questions. (If I interpret your questions correctly, some of what you are talking about will result in undefined behaviour, so for those just compiling and trying to run the test program may not give you a conclusive answer, and that is where we can help.)
-
February 27th, 2010, 07:14 AM
#3
Re: Static casting in C++
Originally Posted by Coder2009
Hi,
I am looking for the answers for the following questions, please:
What will return if the static cast fails for pointers with polymorphic object?
What will return if the static cast fails for reference with polymorphic object?
What will return if the static cast fails for pointers with non-polymorphic object?
What will return if the static cast fails for reference with non-polymorphic object?
When static_cast fails it brokes the compilation since it is performed at compilation time.
I can't understand your other questions at all.
-
February 27th, 2010, 07:49 AM
#4
Re: Static casting in C++
Originally Posted by evlan
When static_cast fails it brokes the compilation since it is performed at compilation time.
Consider this example program:
Code:
#include <iostream>
class X
{
public:
virtual ~X() {}
virtual void foo() const = 0;
};
class Y : public X
{
public:
virtual void foo() const
{
std::cout << "Y::foo() const" << std::endl;
}
};
class Z : public X
{
public:
virtual void foo() const
{
std::cout << "Z::foo() const" << std::endl;
}
};
int main(int argc, char* argv[])
{
Y y;
Z z;
X* x = &y;
if (argc % 2 == 0)
{
x = &z;
}
Y* p = static_cast<Y*>(x);
p->foo();
}
This should compile, and when you run it, it should print "Y::foo() const". However, if you pass an argument to it at the command line when you run it, the static_cast will fail, and the result is undefined.
-
February 27th, 2010, 10:42 AM
#5
Re: Static casting in C++
In my point of view, the "fail" means that something can't be performed. But in that situation, static_cast will convert the base pointer/reference to the child's. I wouldn't consider this behaviour as a fail of the static cast. But indeed this is undefind behavior, since you're trying to access members by invalid type of pointer. But if you wouldn't do this, you won't get an undefined behaviour.
Code:
int main(int argc, char* argv[])
{
Y y;
Z z;
X* x = &y;
Y* p = static_cast<Y*>(x);
std::cout<<"p = "<<p<<"\t&y = "<<&y<<std::endl;
p->foo();
x = &z;
p = static_cast<Y*>(x);
std::cout<<"p = "<<p<<"\t&z = "<<&z<<std::endl;
// x = static_cast<Y*>(&std::cin); //static_cast fails
}
Code:
[evlan@eeepc-900 test]$ ./main
p = 0xbfbe5c7c &y = 0xbfbe5c7c
Y::foo() const
p = 0xbfbe5c78 &z = 0xbfbe5c78
Last edited by evlan; February 27th, 2010 at 11:00 AM.
-
February 27th, 2010, 10:52 AM
#6
Re: Static casting in C++
Originally Posted by evlan
In my point of view, "fail" means that something can't be performed.
Yes, I suppose that in that sense the static_cast will not fail.
Originally Posted by evlan
But in that situation, static_cast will convert the base pointer/reference to the child's. I wouldn't consider this behaviour as fail of the static cast:
Your code example merely demonstrates normal use of static_cast. I am talking about this situation:
Code:
int main(int argc, char* argv[])
{
Z z;
X* x = &z;
Y* p = static_cast<Y*>(x);
std::cout<<"p = "<<p<<"\t&y = "<<&y<<std::endl;
p->foo();
}
Notice that x points to a Z object, but the static_cast attempts to cast x to a pointer to Y.
-
February 27th, 2010, 11:51 AM
#7
Re: Static casting in C++
Notice that x points to a Z object, but the static_cast attempts to cast x to a pointer to Y.
And it will success, since p will contain the address of z. This is not undefined behaviour. Just do not try to access the instance by incorrect type of pointer.
p->foo();
-
February 27th, 2010, 12:03 PM
#8
Re: Static casting in C++
Originally Posted by evlan
And it will success, since p will contain the address of z. This is not undefined behaviour.
No, p may or may not contain the address of z. It probably will, since that makes the most sense for any given implementation, but strictly speaking the result is undefined.
-
February 27th, 2010, 12:33 PM
#9
Re: Static casting in C++
No, p may or may not contain the address of z.
Could you give an reference to the stadard? It would be great if you cite it here.
I thought static_cast can't know that the x contains the address of Z type. But conversion from X pointer to Y is legal.
Last edited by evlan; February 27th, 2010 at 12:36 PM.
-
February 27th, 2010, 12:38 PM
#10
Re: Static casting in C++
Originally Posted by evlan
Could you give an reference to the stadard? It would be great if you cite it here.
Sure.
Originally Posted by C++03 Section 5.2.9 Paragraph 5
An lvalue of type "cv1 B", where B is a class type, can be cast to type "reference to cv2 D", where D is a class derived (clause 10) from B, if a valid standard conversion from "pointer to D" to "pointer to B" exists (4.10), cv2 is the same cv-qualification as, or greater cv-qualification than, cv1, and B is not a virtual base class of D. The result is an lvalue of type "cv2 D." If the lvalue of type "cv1 B" is actually a sub-object of an object of type D, the lvalue refers to the enclosing object of type D. Otherwise, the result of the cast is undefined.
-
February 27th, 2010, 01:17 PM
#11
Re: Static casting in C++
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
|