Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. C++ Style Question

C++ Style Question

Scheduled Pinned Locked Moved The Lounge
questionc++visual-studio
45 Posts 37 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Richard Andrew x64

    If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

    CType* Pointer = NULL;

    vs.

    CType *Pointer = NULL;

    S Offline
    S Offline
    Single Step Debugger
    wrote on last edited by
    #14

    I prefer the second declaration – asterisk next to the variable name for both primitives and objects. The reason is that I very much like the pointers and I don’t want to insult them giving their asterisk to the type. :)

    The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

    1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

      CType* Pointer = NULL;

      vs.

      CType *Pointer = NULL;

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #15

      I prefer the first because it expresses the intent more clearly, but it's "incorrect" for the reason mentioned by others. It's because of this that I never declare multiple variables in one statement. Another thing I'll need to discuss with dmr when I get my time machine working... :~

      1 Reply Last reply
      0
      • N Nish Nishant

        For single variable declarations I'd do :

        int* p1;
        int* p2;

        But for multiple variables this may result in confusion, for example :

        int* p1, p2;

        There, p1 is an int* but p2 is actually an int. So in such cases I'd write :

        int *p1, *p2;

        Of course as far as possible I would avoid multiple variable declarations. That's just not my style.

        Regards, Nish


        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
        My latest book : C++/CLI in Action / Amazon.com link

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #16

        You've directly hit upon my dilemma. The multiple declaration exposes the fact that the asterisk is an attribute of the variable, not the type proper. However, in a single declaration, it somehow makes a lot of sense to indicate that you're declaring a type-pointer. :confused:

        C 1 Reply Last reply
        0
        • N Nish Nishant

          For single variable declarations I'd do :

          int* p1;
          int* p2;

          But for multiple variables this may result in confusion, for example :

          int* p1, p2;

          There, p1 is an int* but p2 is actually an int. So in such cases I'd write :

          int *p1, *p2;

          Of course as far as possible I would avoid multiple variable declarations. That's just not my style.

          Regards, Nish


          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
          My latest book : C++/CLI in Action / Amazon.com link

          B Offline
          B Offline
          Big Daddy Farang
          wrote on last edited by
          #17

          Nishant Sivakumar wrote:

          avoid multiple variable declarations

          Dr. Plum and I agree. :)

          BDF People don't mind being mean; but they never want to be ridiculous. -- Moliere

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

            CType* Pointer = NULL;

            vs.

            CType *Pointer = NULL;

            B Offline
            B Offline
            Big Daddy Farang
            wrote on last edited by
            #18

            What I was taught in my first C++ class is:

            CType* Pointer = NULL; // proper style for C++

            CType *Pointer = NULL; // proper style for C

            Although this may be the instructor's opinion. It's what I've been doing ever since, however.

            BDF People don't mind being mean; but they never want to be ridiculous. -- Moliere

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

              CType* Pointer = NULL;

              vs.

              CType *Pointer = NULL;

              K Offline
              K Offline
              Kevin McFarlane
              wrote on last edited by
              #19

              First for same reason as Joe Woodbury.

              Kevin

              1 Reply Last reply
              0
              • L Lost User

                I prefer the first. Same with references:

                Object& obj = someFunc();

                as opposed to:

                Object &obj = someFunc();

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #20

                Ditto.

                Kevin

                1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

                  CType* Pointer = NULL;

                  vs.

                  CType *Pointer = NULL;

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #21

                  You can solve the problem by dropping the space. More seriously, putting more than one variable in the declaration (one pointer, one not) should show the way to do it. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  1 Reply Last reply
                  0
                  • J Joe Woodbury

                    I prefer the first because it groups the description of the type and the variable name separately.

                    Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                    A Offline
                    A Offline
                    Anna Jayne Metcalfe
                    wrote on last edited by
                    #22

                    Ditto. The second one always looks like a dereference to me - which it isn't.

                    Anna :rose: Having a bad bug day? Tech Blog | Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"

                    V 1 Reply Last reply
                    0
                    • N Nish Nishant

                      For single variable declarations I'd do :

                      int* p1;
                      int* p2;

                      But for multiple variables this may result in confusion, for example :

                      int* p1, p2;

                      There, p1 is an int* but p2 is actually an int. So in such cases I'd write :

                      int *p1, *p2;

                      Of course as far as possible I would avoid multiple variable declarations. That's just not my style.

                      Regards, Nish


                      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                      My latest book : C++/CLI in Action / Amazon.com link

                      A Offline
                      A Offline
                      Anna Jayne Metcalfe
                      wrote on last edited by
                      #23

                      I avoid multiple pointer declarations on the same line as I always initialise them (never leave them dangling):

                      int* p1 = NULL;
                      int* p2 = NULL;

                      Anna :rose: Having a bad bug day? Tech Blog | Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"

                      1 Reply Last reply
                      0
                      • Richard Andrew x64R Richard Andrew x64

                        If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

                        CType* Pointer = NULL;

                        vs.

                        CType *Pointer = NULL;

                        S Offline
                        S Offline
                        Stuart Dootson
                        wrote on last edited by
                        #24

                        I prefer the first one's aesthetics - but it can hide the way C type-decls are actually decoded, and as Nish said, the multiple item declaration thing is an issue. The other alternative, of course, is to encapsulate the pointer-ness in a typedef:

                        typedef CType* CTypePtr;
                        CTypePtr Pointer = NULL;

                        Helps especially with function pointers - which is easier to understand:

                        int (*myFn)(double, int);

                        or

                        typedef int (*MyFnPtr)(double, int);

                        MyFnPtr myFn;

                        ? I guess some amount of the SPARK Ada development I've done (SPARK doesn't allow anonymous type defs) has rubbed off on me. Who'da thunk it...

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        1 Reply Last reply
                        0
                        • J Joe Woodbury

                          I prefer the first because it groups the description of the type and the variable name separately.

                          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #25

                          Same here.

                          Visit http://www.notreadytogiveup.com/[^] and do something special today.

                          1 Reply Last reply
                          0
                          • Richard Andrew x64R Richard Andrew x64

                            If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

                            CType* Pointer = NULL;

                            vs.

                            CType *Pointer = NULL;

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #26

                            It belongs next to the type name because it contributes to the type specification.

                            S 1 Reply Last reply
                            0
                            • Richard Andrew x64R Richard Andrew x64

                              If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

                              CType* Pointer = NULL;

                              vs.

                              CType *Pointer = NULL;

                              J Offline
                              J Offline
                              Jonas Hammarberg
                              wrote on last edited by
                              #27

                              Alternative 1, with a slight twitch

                              CType* Pointer(0);

                              rgds *I don't do multiple variable declarations*/Jonas

                              1 Reply Last reply
                              0
                              • Richard Andrew x64R Richard Andrew x64

                                If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

                                CType* Pointer = NULL;

                                vs.

                                CType *Pointer = NULL;

                                N Offline
                                N Offline
                                Niall Joubert
                                wrote on last edited by
                                #28

                                Depends on where you want the emphasis to be. Personally, I prefer the

                                CType* Pointer

                                It reads more clearly that the variable Pointer is of "pointer type." It does mean though that it is difficult to declare multiple variables on a single line, but frankly, I think it is better style to only declare a single variable per line. The typedef mentioned in a previous post is very neat. I've used this a few times, it's even better at showing the "pointer type." If you can, prefer this.

                                1 Reply Last reply
                                0
                                • D Dan Neely

                                  I've been burned by multiple declarations enough that I always put the * on the variable name for clarity.

                                  It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies

                                  H Offline
                                  H Offline
                                  hairy_hats
                                  wrote on last edited by
                                  #29

                                  Likewise.

                                  1 Reply Last reply
                                  0
                                  • Richard Andrew x64R Richard Andrew x64

                                    If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

                                    CType* Pointer = NULL;

                                    vs.

                                    CType *Pointer = NULL;

                                    D Offline
                                    D Offline
                                    dazfuller
                                    wrote on last edited by
                                    #30

                                    Personally I prefer the first option: int* a It doesn't look like I'm trying to dereference an existing variable that way and I think it makes it easier to read. Also I avoid writing multiple definitions on the same line, again it makes it easier to read in my opinion and it means that I don't have to change my coding style for pointer declaration.

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      It belongs next to the type name because it contributes to the type specification.

                                      S Offline
                                      S Offline
                                      Steve Thresher
                                      wrote on last edited by
                                      #31

                                      Wrong! It's a modifier for the variable like const. which is why you put it with the variable, not the data type.

                                      1 Reply Last reply
                                      0
                                      • Richard Andrew x64R Richard Andrew x64

                                        If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?

                                        CType* Pointer = NULL;

                                        vs.

                                        CType *Pointer = NULL;

                                        B Offline
                                        B Offline
                                        Brady Kelly
                                        wrote on last edited by
                                        #32

                                        C/C++ was my first introduction to programming years after some dabbling in Sinclair Basic. This was before Google, and even before ready access, for me, to forums etc. and for a long time, I thought there was a difference.

                                        You really gotta try harder to keep up with everyone that's not on the short bus with you. - John Simmons / outlaw programmer.

                                        1 Reply Last reply
                                        0
                                        • C Chris Losinger

                                          i always use the 2nd, out of habit. the first would be clearer except you can do this:

                                          CType* ptr, nonPtr;

                                          but the * doesn't apply to all items in the var list. therefore it's not really part of the type, it's really tied to the individual variable. but that's not why i don't use it.

                                          image processing toolkits | batch image processing

                                          F Offline
                                          F Offline
                                          fboule
                                          wrote on last edited by
                                          #33

                                          I couldn't have explained it better. I like to consider the asterisk as a modifier to the type. It does actually belong neither to the type, nor to the variable. Its name is ptr and not *ptr. Another example is:

                                          int i = 123;
                                          int &j = i;

                                          It becomes even more unclear if you consider &j as the variable name. Eventually, an alternate way to consider it is with parenthesis:

                                          CType (*ptr), nonPtr;

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups