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 Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    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;

    M C J N N 28 Replies 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;

      M Offline
      M Offline
      M dHatter
      wrote on last edited by
      #2

      Depends on which one is male and which one is female :laugh: ...asterisk

      "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein

      modified on Wednesday, June 10, 2009 3:51 PM

      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;

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        CType * pointer = NULL;

        Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

        K 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
          Joe Woodbury
          wrote on last edited by
          #4

          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 L 2 Replies 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
            Nemanja Trifunovic
            wrote on last edited by
            #5

            I use the former style, mostly because I learned C++ from the Stroustrup's book, and that's what he does. BTW, there is another style you didn't mention:

            CType * Pointer = NULL;

            Programming Blog utf8-cpp

            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;

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              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

              D Richard Andrew x64R B A 4 Replies 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
                #7

                I prefer the first. Same with references:

                Object& obj = someFunc();

                as opposed to:

                Object &obj = someFunc();

                K J 2 Replies 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;

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #8

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

                    0 Offline
                    0 Offline
                    0x3c0
                    wrote on last edited by
                    #9

                    I prefer the latter, because in my mind, Pointer is already a CType. The asterisk simply adds the notice that the variable just points to a CType, instead of actually being the CType. Plus, it looks a little neater

                    Between the idea And the reality Between the motion And the act Falls the Shadow

                    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;

                      H Offline
                      H Offline
                      Hans Dietrich
                      wrote on last edited by
                      #10

                      What if you wanted to declare both a non-pointer and a pointer to CType on the same line? [Not something I favor, but nevertheless legal]. In other words, take the two lines

                      CType* v1;
                      CType v2;

                      and combine them into one line. If you wrote the one line like this:

                      CType* v1, v2; // only v1 is pointer

                      I think it would be natural (and also incorrect) to think that both v1 and v2 were pointers to CType, whereas if you always associate the dereference operator with the variable, this confusion may be minimized:

                      CType *v1, v2; // only v1 is pointer

                      Best wishes, Hans


                      [Hans Dietrich Software]

                      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

                        D Offline
                        D Offline
                        Dan Neely
                        wrote on last edited by
                        #11

                        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 1 Reply Last reply
                        0
                        • N Nemanja Trifunovic

                          I use the former style, mostly because I learned C++ from the Stroustrup's book, and that's what he does. BTW, there is another style you didn't mention:

                          CType * Pointer = NULL;

                          Programming Blog utf8-cpp

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

                          I call this style the “MindChanger”.

                          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
                          • C Christian Graus

                            CType * pointer = NULL;

                            Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

                            K Offline
                            K Offline
                            kinar
                            wrote on last edited by
                            #13

                            this is how I do it.

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