Thread: operators???

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    operators???

    I recently saw in some C++ books these operators :
    dynamic_cast
    static_cast
    reinterpret_cast
    const_cast
    typeid

    I so these same operators in about 2 - 4 books (Beginers ).
    But they are seen ony in the appendices and no actual instances of their use are given or explained.
    If they are type casting/ type conversion operators, how come they are not seen in any books for beginers?
    Please forgive me, but is there any difference b/w type casting/ type conversion? (^_^)!
    P.S. I fact I have never seen any of these operators in use in my really short life as a 'programmer'.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Please do not quote me on this, but I have heard of those, and have been told they are used in windows programing more than anything, but I am not completley sure

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Casting of things are strictly for experienced coders. If you have to ask about them, you are not a member of that group. The basically allow you to violate the protection built into C++ and that is only very rarely a requirement (and often for very low-level or systems programming). They are far from specific to Windows, but since many of the Windows API calls are in C and use void pointers, it is a necessity.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The casts you listed are the standard form of type casting in C++. You can still use the C style type casts if so desired. Up to date books/tutorials covering C++ should use the standard C++ cast syntax, though they are a bit klunkier.

    If I remember correctly, typeid allows you determine the type of an object. This can become important with polymorphism.

    To my knowledge, type casting and type conversions accomplish pretty much the same task, but if you find yourself type casting a given class to another type routinely, you can save time/typing by using a conversion operator.
    You're only born perfect.

  5. #5
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    The basically allow you to violate the protection built into C++
    In that case I surely don't want to mess with that...

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    All those things are standard C++ operators. They might be used in windows programming, but that's only because they are standard C++ and some windows programmers presumably do things that require their behaviour.

    dynamic_cast<T>(v) converts the expression v so it is of type T. T can only be a pointer or reference to a complete class type (i.e. not int, double, etc and not a forward declaration for a class) or pointer to cv void (cv means it can be qualified as const or volatile). The logic of how dynamic_cast works is (straight from the standard);
    -- If, in the most derived object pointed (referred) to by v, v points (refers) to a public base class subobject of a T object, and if only one object of type T is derived from the subobject pointed (referred) to by v, the result is a pointer (an lvalue referring) to that T object.
    -- Otherwise, if v points (refers) to a public base class subobject of the most derived object, and the type of the most derived object has an unambiguous public base class of type T, the result is a pointer (an lvalue referring) to the T subobject of the most derived object.
    -- Otherwise, the runtime check fails.

    If T is a pointer, a failed dynamic_cast yields a NULL pointer. If T is a reference, a std::bad_cast exception is thrown.

    For example;
    Code:
    class B {};
    class D : public B {};
    
    int main()
    {
        B *x = new D;
       if (dynamic_cast<D *>(x) == NULL) std::cout << "x is of type D\n";
    }
    This code will always report that x is of type D.

    The important thing about dynamic_cast<> is that it provides a runtime check of type of an object. Hence it is sometimes used in object libraries (eg under windows it might be used to check that a pointer to Widget actually pointers to a Button).

    static_cast<T>(v) converts expression v to be of type T, at compile time.

    reinterpret_cast<T>(v) is used to convert expressions v to type T. It only works for specific cases of converting integral values to pointers, and pointers so they can be stored in integral types (int, unsigned, etc). The behaviour is compiler dependent but can be used (say) to print a value of a pointer. If reinterpret_cast is used to convert a pointer to an integral type, and used again to convert back to the pointer type, then the original pointer will come back. Information can, however, be lost if the pointer and integral types are of different size (eg a 16 bit int can't hold a 32 bit pointer).

    Unlike the other _cast, const_cast is used to change constness. For example,
    Code:
    int do_something(const int &x)
    {
        int &y = const_cast<int &>(x);
        y = 42;    // now allowed to modify x
    }
    Keep in mind that the above code yields undefined behaviour. It allows the function to modify a const argument. The const_cast<> does not make changing x safe; it simply stops the compiler from complaining at the attempt to change x (though y). The most common usage of const_cast is when using an old library in a new one;
    Code:
    int SomeFunctionFromOldLibrary(int &x)
    {
         // this function does not actually change x
    }
    
    int SomeFunction(const int &x)
    {
        SomeFunctionFromOldLibrary(const_cast<int &>(x));  // allow us to call SomeFunctionFromOldLibrary() as we trust it not to modify x
    }
    typeid(x) gives information (in a structure of type std::type_info, which is declared in the header <typeinfo>) about the type of an object. It only works for class types. type_info objects can be compared (eg they are not equal if two objects are of different types) and a name() method gives a string which identifies the type (the content of that string is compiler dependent though).

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb A couple of good references:

    Volume I, chapter 3 of Thinking In C++, by Bruce Eckel (free download) has some good coverage of cast. Chapter 15 covers typeid.

    Dinkumware.com (free online) is a complete C++ reference.

    You can download the actual ANSI/ISO C and C++ Language Standards in PDF format from ANSI for about $20 each (you need 'em both to cover the whole C++ language).

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I wouldn't recommend the C++ standard for those trying to learn basics of the language; it is not written for that. However, it is very useful as a reference, and to check specifics, once you get going.

    The link given by Stoned_Coder is not actually the standard; on a quick look it is based on a reasonably late draft.

    This link (which I found on one the pages at Bjarne Stroustrup's home page at AT&T) provides access to a reasonably late draft of the C++ standard. It is not the standard, but you would have to look very closely to identify the technical differences.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    class B {};
    class D : public B {};
    
    int main()
    {
        B *x = new D;
       if (dynamic_cast<D *>(x) == NULL) std::cout << "x is of type D\n";
    }
    2 small fix...
    Code:
    class B {
        virtual void f();
    };
    class D : public B {
        virtual void f();
    };
    
    int main()
    {
        B *x = new D;
       if (dynamic_cast<D *>(x) != NULL) std::cout << "x is of type D\n";
    }

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by xErath
    2 small fix...
    Ah, yes; I left that out. dynamic_cast<> only works for converting a base class pointer or reference to a derived class if the base class is polymorphic i.e. it has a virtual member function.

    There is not actually a need for the derived class to override that virtual function though, so your first fix is all that is required for dynamic_cast<> to work.

  12. #12
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Thanks for the advice, but I told you I'm sticking with the type conversion for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  4. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 07:35 PM