New typecast Style in C++?

This is a discussion on New typecast Style in C++? within the C++ Programming forums, part of the General Programming Boards category; I was going through C++ Primer Plus, 5th ed. I noticed under typecasting it says use the following for type ...

  1. #1
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    New typecast Style in C++?

    I was going through C++ Primer Plus, 5th ed.

    I noticed under typecasting

    it says use the following for type casts in C++

    typename (value) // new C++

    mentions the fact it was meant to look like a function call.

    So

    int num;

    num = int (19.99) + int (11.99);
    cout << num;

    I thought the new way to cast in Standard C++ is using static_cast like below

    float f = 100.0;
    int a = static_cast<int>(f);


    Have any of you heard of this "new casting style"

    typename (value) // new C++


    Mr. C.
    Mr. C: Author and Instructor

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    It's hardly new, it's been around since C++ was standardized. T(x) is sugar for a static_cast.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    learn to either RTFM or google for questions like this. static casts were part of the oritinal c++ language. If you don't know what it is, then you need to either look it up in a textbook or search google for it. There are two other kinds of casts as well which you should learn about.

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Ancient Dragon,

    I did do a search and was aware of the C style and static_cast as well as the other kinds of casts.

    I just never seen it spelled out like this in textbooks.

    Mr. C.
    Mr. C: Author and Instructor

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    That style of casting is nice because it follows the constructor look and feel.

    I.E.
    Code:
    class Bob {
      public:
        Bob(int);
    };
    Would allow you to do
    Code:
    Bob(10);
    int x;
    Bob(x);
    The only time it gets annoying is with two word type names
    Code:
    unsigned long (x);
    just doesn't work Have to do
    Code:
    (unsigned long)(x);
    And Ancient Dragon you haven't been here long enough to pick on Mister C.

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    And Ancient Dragon you haven't been here long enough to pick on Mister C.
    That's Good to know...
    Mr. C: Author and Instructor

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In general, though, I recommend you prefer the static_cast style. It's easier to find in the source.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Thantos
    And Ancient Dragon you haven't been here long enough to pick on Mister C.
    Old farts like me can pick on anyone I like, thank you very much.

    Code:
     num = int (19.99) + int (11.99);
    I've seen it on very rare occasions. I don't like it and don't use it.
    Last edited by Ancient Dragon; 09-19-2005 at 06:04 AM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You may be an old fart in real life, but you're an infant on this board.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Ohhh touchy today! Some people can't take critizism, even from newer members.

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Some people can't take critizism
    You don't seem to be taking criticism that well...

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    In general you sohuld avoid casting as much as possible. The only time One really needs to cast in C++ is to handle conversions from void* (to void* are still implicit). But in that case you shouldn't be using void*'s at all.

    Casting is really one of the worst things C++ has. That and pointers. Being able to forcibly convert an object to another type never results in good things happening.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by orbitz
    In general you sohuld avoid casting as much as possible. The only time One really needs to cast in C++ is to handle conversions from void* (to void* are still implicit). But in that case you shouldn't be using void*'s at all.

    Casting is really one of the worst things C++ has. That and pointers. Being able to forcibly convert an object to another type never results in good things happening.
    Really? Hmm so if I want to truncate my results in the middle of a math formula what should I do? Now you might say that my example isn't a "real life" example but I shall counter with: Significant figures which involves a lot of rounding and/or truncation.

    I have a class that store rational numbers as the quotient of two integers. I want to include the ability to get a decimial representation that is a double. I also want it to be clean and simple to use and allow the same style of programming that has been done in the last 20 years. Go!

    As to pointers: I want to apply a user defined function to each item in a container. Create a generic function to do this without using a pointer. And go!

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    As to pointers: I want to apply a user defined function to each item in a container. Create a generic function to do this without using a pointer. And go!
    You seem to be confusing the requirement of pointeres to accomplish things in C++ with pointers being a good thing. Just because C++ lacks decent means of accomplishing the task doesn't mean it's a very good way.

    Ok, I take back my rather short-sighted statement about casting. Anything beyond static_cast should be avoided (although dynamic_cast has some uses) *generally*. I believe the results of a reinterpret_cast is generally implementation defined (correct me if I'm wrong). Removing const or volatile is almost always wrong. But i still stand by my "you sohuld avoid casting in C++ as much as possible". Using casting to suppress an error from your compiler is almost always wrong, which is what the majority of instances of casting I see appear to do.

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Although, overloading operator() and using references would allow you to do that without pointers, wouldn't it? But taht certianly is a poor solution IMO.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 04:18 PM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 12:21 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21