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. General Programming
  3. C / C++ / MFC
  4. Pointer symbol position and constant member functions

Pointer symbol position and constant member functions

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 4 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.
  • C Offline
    C Offline
    Christian Flutcher
    wrote on last edited by
    #1

    I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?

    int* intPtr

    OR

    int *intPtr;

    IMO, the second one is more clear when we declare something like this.

    int *intPtr1,intPtr2;

    2 - Is it a good practice to append const with member function that doesn't modify any member variables?

    int foo::sampleMethod() const{
    }

    I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.

    S T I 3 Replies Last reply
    0
    • C Christian Flutcher

      I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?

      int* intPtr

      OR

      int *intPtr;

      IMO, the second one is more clear when we declare something like this.

      int *intPtr1,intPtr2;

      2 - Is it a good practice to append const with member function that doesn't modify any member variables?

      int foo::sampleMethod() const{
      }

      I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      I personally prefer the int* intPtr; But I declare a single variable in a line. This way I can add a comment for each variable I use. I do add const to each member function that doesn't modify member variables. The problem is you can almost never predict how the class is going to be used at a later time. Once I had to add const to loads of member function for about 10-20 classes. Also, I am not sure, but in certain cases adding const might help compiler to produce more optimized code. -Saurabh

      C 1 Reply Last reply
      0
      • C Christian Flutcher

        I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?

        int* intPtr

        OR

        int *intPtr;

        IMO, the second one is more clear when we declare something like this.

        int *intPtr1,intPtr2;

        2 - Is it a good practice to append const with member function that doesn't modify any member variables?

        int foo::sampleMethod() const{
        }

        I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        you have an interview, don't you ? :)

        Christian Flutcher wrote:

        1 - Which is the preferred way of using the pointer symbol ?

        for me, I like to declare every variable separatedly, and I don't like to mix declarations of different types. this way, I prefer using int* pi; syntax because I see immediately that pi is of type int*.

        Christian Flutcher wrote:

        2 - Is it a good practice to append const with member function that doesn't modify any member variables?

        definitely, yes ! because the one who write the class is not always the one who will use it, a class definition has to be clear, and has to mean what the class is designed for. When you have an accessor which just does a "get", the it's obviously not modifying the object, and should be declared as const.

        Christian Flutcher wrote:

        I understand why const member functions exists. But if we are not planning to make the class object as constant,

        humm, it seems to me that you don't fully understand the thing. making a function member constant doesn't mean every function members have to be consts, and it doesn't mean either that the object will be used as a constant. If you'd like to say that the object is used as a constant, then you would have to do like this :

        class foo { ... };

        const foo f(/* some initialization parameters */);

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        C 1 Reply Last reply
        0
        • C Christian Flutcher

          I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?

          int* intPtr

          OR

          int *intPtr;

          IMO, the second one is more clear when we declare something like this.

          int *intPtr1,intPtr2;

          2 - Is it a good practice to append const with member function that doesn't modify any member variables?

          int foo::sampleMethod() const{
          }

          I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          1/ I normally do:

          int *pInt;

          2/ the question you ask is why I never mix those. And if the variable it at all significant, it gets its own line. I use long variable names - thanks to intellitext they're just as easy to type, and a lot easier to read. Only loop vars get bunched together. int i,j,k; 3/ I try to use const on member functions that *shouldn't* modify the state, not whether I think I might do so. It gives me the freedom to change my mind in one direction, and makes it harder to code mistakes in the other. Iain.

          C 1 Reply Last reply
          0
          • T toxcct

            you have an interview, don't you ? :)

            Christian Flutcher wrote:

            1 - Which is the preferred way of using the pointer symbol ?

            for me, I like to declare every variable separatedly, and I don't like to mix declarations of different types. this way, I prefer using int* pi; syntax because I see immediately that pi is of type int*.

            Christian Flutcher wrote:

            2 - Is it a good practice to append const with member function that doesn't modify any member variables?

            definitely, yes ! because the one who write the class is not always the one who will use it, a class definition has to be clear, and has to mean what the class is designed for. When you have an accessor which just does a "get", the it's obviously not modifying the object, and should be declared as const.

            Christian Flutcher wrote:

            I understand why const member functions exists. But if we are not planning to make the class object as constant,

            humm, it seems to me that you don't fully understand the thing. making a function member constant doesn't mean every function members have to be consts, and it doesn't mean either that the object will be used as a constant. If you'd like to say that the object is used as a constant, then you would have to do like this :

            class foo { ... };

            const foo f(/* some initialization parameters */);

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            C Offline
            C Offline
            Christian Flutcher
            wrote on last edited by
            #5

            toxcct wrote:

            you have an interview, don't you ?

            :) No I don't have. I just finished reading "Thinking in C++" and about to start a project in C++. So thought of getting some expert advice on those points. Thanks for you help. It was really helpful.

            1 Reply Last reply
            0
            • I Iain Clarke Warrior Programmer

              1/ I normally do:

              int *pInt;

              2/ the question you ask is why I never mix those. And if the variable it at all significant, it gets its own line. I use long variable names - thanks to intellitext they're just as easy to type, and a lot easier to read. Only loop vars get bunched together. int i,j,k; 3/ I try to use const on member functions that *shouldn't* modify the state, not whether I think I might do so. It gives me the freedom to change my mind in one direction, and makes it harder to code mistakes in the other. Iain.

              C Offline
              C Offline
              Christian Flutcher
              wrote on last edited by
              #6

              Perfect ! Thanks for the help Iain.

              1 Reply Last reply
              0
              • S Saurabh Garg

                I personally prefer the int* intPtr; But I declare a single variable in a line. This way I can add a comment for each variable I use. I do add const to each member function that doesn't modify member variables. The problem is you can almost never predict how the class is going to be used at a later time. Once I had to add const to loads of member function for about 10-20 classes. Also, I am not sure, but in certain cases adding const might help compiler to produce more optimized code. -Saurabh

                C Offline
                C Offline
                Christian Flutcher
                wrote on last edited by
                #7

                Saurabh.Garg wrote:

                But I declare a single variable in a line. This way I can add a comment for each variable I use.

                Saurabh.Garg wrote:

                The problem is you can almost never predict how the class is going to be used at a later time.

                Good points.

                Saurabh.Garg wrote:

                I am not sure, but in certain cases adding const might help compiler to produce more optimized code.

                Do you have any articles/books which explains this? Thanks for your help

                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