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;

    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