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;

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

                              U Offline
                              U Offline
                              User 4595697
                              wrote on last edited by
                              #40

                              Just use c# instead

                              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;

                                U Offline
                                U Offline
                                uzziah0
                                wrote on last edited by
                                #41

                                Since I mostly work with existing code, I try to stay with the style already in the existing code; unless there is something really bad or goofy. I would follow what others have already done in the existing code. Most of the work I do has informal standards, where you only put one variable declaration on a single line. By practice (meaning there is no standard) we out the * on the variable:

                                int *some = NULL;
                                int *other = NULL;

                                1 Reply Last reply
                                0
                                • L Lost User

                                  I prefer the first. Same with references:

                                  Object& obj = someFunc();

                                  as opposed to:

                                  Object &obj = someFunc();

                                  J Offline
                                  J Offline
                                  jasmin_m
                                  wrote on last edited by
                                  #42

                                  It has more style :) Type* pointer=NULL;

                                  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
                                    Craig Atwood
                                    wrote on last edited by
                                    #43

                                    Its all up to your personal preference, the only time it matters is when you declare many variables in 1 line e.g int* first, second here both variables are type int* int *first, second here first is int* and second is primitive int Personally i use int* ithe reason being that the data type is more clear when reading the code

                                    http://www.4cgroup.co.za/

                                    1 Reply Last reply
                                    0
                                    • E etkid84

                                      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 Offline
                                      R Offline
                                      ricecake
                                      wrote on last edited by
                                      #44

                                      Yes, he gives his reasoning here: http://www.research.att.com/~bs/bs_faq2.html#whitespace[^]

                                      -- Marcus Kwok

                                      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
                                        Stephen F Heffner
                                        wrote on last edited by
                                        #45

                                        1. C and C++ have a basic style that says variables are declared in the same format in which they're used. Other examples are arrays and function pointers. To be consistent with that style, you would use "Ctype *foo", not "Ctype* foo" or "Ctype * foo". 2. The point about multiple decls is well made, e.g. "Ctype *foo, *bar". This illustrates the real nature of the "*" in the decl, which argues for "Ctype *foo" for a single decl. 3. IMHO what's more important is a naming convention for pointers. I maintain 1/2 million code lines of C with heavy pointer use, and I never have dereferencing bugs, because we use such a convention: "p_" prefix on names to indicate a pointer ("pp_" for double pointers, etc.), i.e. "p_foo" in the example above. (Another convention is a suffix of "P", i.e. "fooP" in the example.) Let's say you see "p_foo = *pp_bar;" in some code. Once your eyes learn to balance the levels using the convention, you can see that this is correct, whereas "p_foo = **pp_bar;" looks wrong. Stephen F. Heffner, President | Phone: +1(480)626-5503 ------------ | Fax: +1(480)626-7618 Pennington Systems Incorporated | Email: Heffner@Pennington.com 8655 East Via de Ventura, Suite G200 | Web: http://WWW.Pennington.com Scottsdale, Arizona 85258-3321 USA | XTRAN: A software engineering meta-tool

                                        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