Member function definition syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsi
    New Member
    • Jul 2007
    • 51

    Member function definition syntax

    Hi,
    I looked a while at this but couldnt convice myself of the following reasons in the code.

    [code=cpp]

    class A{
    int a;
    public:
    A():a(0){}
    void func() {int A::*ptr = &A::a;}
    void func1() {int *ptr = &a;}
    //void func2() {int *ptr = &A::a;}----> compiler complains
    //void func3() {int A::*ptr = &a;}----> compiler complains
    };


    1. The compiler expects the dereference operator after the scope resolution operator as in (A::*ptr) but complains when i try to use the same convention like (A::&A). Does this has any specifc reason or it is that the implementor liked it this way (Me confused bcos the :: operator has a higher precedence in both the cases).

    2. Does the func1() and func() differ in any way bcos of the use of (A::) operator in the func() definition. I suppose that the use of (A::) is redundant as it is implicit but I am doubtful bcos the compiler complains about func2() and func3().
    why is that func2() and func3() are invalid syntax.

    Thanks in advance.
    gsi.

    [/code]
  • sanctus
    New Member
    • Mar 2007
    • 84

    #2
    Originally posted by gsi
    Hi,
    I looked a while at this but couldnt convice myself of the following reasons in the code.

    [code=cpp]

    class A{
    int a;
    public:
    A():a(0){}
    void func() {int A::*ptr = &A::a;}
    void func1() {int *ptr = &a;}
    //void func2() {int *ptr = &A::a;}----> compiler complains
    //void func3() {int A::*ptr = &a;}----> compiler complains
    };


    1. The compiler expects the dereference operator after the scope resolution operator as in (A::*ptr) but complains when i try to use the same convention like (A::&A). Does this has any specifc reason or it is that the implementor liked it this way (Me confused bcos the :: operator has a higher precedence in both the cases).

    2. Does the func1() and func() differ in any way bcos of the use of (A::) operator in the func() definition. I suppose that the use of (A::) is redundant as it is implicit but I am doubtful bcos the compiler complains about func2() and func3().
    why is that func2() and func3() are invalid syntax.

    Thanks in advance.
    gsi.

    [/code]
    I'm not sure about this but shouldn't you define ptr as an element of the class before using it?

    Comment

    • gsi
      New Member
      • Jul 2007
      • 51

      #3
      Hi,
      ptr is local to the member function and is not a class member. The compiler allows the use of :: operator although the member variable a is visible at that point , but it expects the use of :: operator for even the local variable ptr. I am doubtful about this.

      Thanks,
      gsi.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by gsi
        ptr is local to the member function and is not a class member.
        If ptr is local to the member function, then it does not have the scope of A::. Therefore, A::ptr would require ptr to be a class member.

        The local ptr would be:

        [code=cpp]
        void func3() {int* ptr = &a;}
        [/code]

        Here you need to be sure the compiler takes the address of the correct token. You should have:

        [code=cpp]
        void func2() {int *ptr = &(A::a);}
        [/code]

        Or better, the real code should be:
        [code=cpp]
        void func2() {int *ptr = &this->a;}
        [/code]

        Comment

        • gsi
          New Member
          • Jul 2007
          • 51

          #5
          Hi,
          This class compiles and runs fine.

          [code=cpp]
          class A{
          private:
          int a;
          public:
          A():a(1) { }
          void func() {int A::*ptr = &A::a;}
          };

          int main() {
          A a;
          }
          [/code]

          I completely agree with you, But here ptr is not declared as a member variable of class A, why is the compiler allowing such a definition. Also I could'nt figure out a syntax to print out the ptr inside the function func() (ie the value of a). is it that this is a wrong syntax ?, but the compiler is unaware of it.

          I understand that this sort of thing is wierd to do , but I saw this to be used in this IBM tutorial , I couldn't make out why point_i2 is defined such a way (prefixed with A::) even though it is a local pointer.


          Thanks in advance ,
          gsi.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by gsi
            class A{
            private:
            int a;
            public:
            A():a(1) { }
            void func() {int A::*ptr = &A::a;}
            };

            int main() {
            A a;
            }
            Above ptr is defined as a pointer to a member of A that is an int. That's OK. Then you initialize it with the address of a member of A that is an int. That's OK.

            This is not OK:
            Code:
            class A{
            private:
              int a;
            public:
              A():a(1) { }
              void func() {int A::*ptr = &a;}
            };
            Here ptr is defined as a pointer to a member of A that is an int. That's OK. But initializing it with the address of a variable that may not be a member of A is NOT OK.

            Comment

            • gsi
              New Member
              • Jul 2007
              • 51

              #7
              Hi,
              That make's sence, Thanks for the info.

              Thanks,
              gsi.

              Comment

              • abhishek batra
                New Member
                • Aug 2007
                • 1

                #8
                Originally posted by gsi
                Hi,
                I looked a while at this but couldnt convice myself of the following reasons in the code.

                [code=cpp]

                class A{
                int a;
                public:
                A():a(0){}
                void func() {int A::*ptr = &A::a;}
                void func1() {int *ptr = &a;}
                //void func2() {int *ptr = &A::a;}----> compiler complains
                //void func3() {int A::*ptr = &a;}----> compiler complains
                };


                1. The compiler expects the dereference operator after the scope resolution operator as in (A::*ptr) but complains when i try to use the same convention like (A::&A). Does this has any specifc reason or it is that the implementor liked it this way (Me confused bcos the :: operator has a higher precedence in both the cases).

                2. Does the func1() and func() differ in any way bcos of the use of (A::) operator in the func() definition. I suppose that the use of (A::) is redundant as it is implicit but I am doubtful bcos the compiler complains about func2() and func3().
                why is that func2() and func3() are invalid syntax.

                Thanks in advance.
                gsi.

                [/code]
                Please ignore this post.

                Comment

                Working...