CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2009
    Posts
    2

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2010
    Location
    Russia GMT+3
    Posts
    12

    Re: Static casting in C++

    Quote Originally Posted by Coder2009 View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Static casting in C++

    Quote 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 &#37; 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Feb 2010
    Location
    Russia GMT+3
    Posts
    12

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Static casting in C++

    Quote 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.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Feb 2010
    Location
    Russia GMT+3
    Posts
    12

    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();

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Static casting in C++

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Feb 2010
    Location
    Russia GMT+3
    Posts
    12

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Static casting in C++

    Quote Originally Posted by evlan
    Could you give an reference to the stadard? It would be great if you cite it here.
    Sure.
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Feb 2010
    Location
    Russia GMT+3
    Posts
    12

    Re: Static casting in C++

    Sure.
    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured