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.
  • 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
                              • Richard Andrew x64R Richard Andrew x64

                                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 Offline
                                C Offline
                                CannibalSmith
                                wrote on last edited by
                                #34

                                int* p1,* p2;

                                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;

                                  G Offline
                                  G Offline
                                  Gary Wheeler
                                  wrote on last edited by
                                  #35

                                  The second, since the '*' associates with the variable name, not the type.

                                  Software Zen: delete this;

                                  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
                                    Bob1000
                                    wrote on last edited by
                                    #36

                                    If I see the first I know who wrote the code lives by the axiom 'why make things clear when they can be difficult’ :)

                                    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;

                                      E Offline
                                      E Offline
                                      etkid84
                                      wrote on last edited by
                                      #37

                                      i would follow the czar of c++. you might want to consider keeping one style/idiom for programming in c, and follow dr. stroustrup's examples for c++. kind regards,

                                      David

                                      R 1 Reply Last reply
                                      0
                                      • A Anna Jayne Metcalfe

                                        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 Offline
                                        V Offline
                                        VEMS
                                        wrote on last edited by
                                        #38

                                        Agree

                                        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
                                          #39

                                          I always thought

                                          CType *Pointer

                                          made more sense, since

                                          *Pointer

                                          is actually a CType variable, you're just giving it a weird name that involves a pointer. But this is just a noobie making his first post :P

                                          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