Nonstatic member example?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fl

    Nonstatic member example?

    Hi,
    There is a question about nonstatic member. C++ primer says: A
    nonstatic member is restricted to being declared as a pointer or
    reference to an object of its class. It only gives an example of
    pointer *b.

    class Bar {
    public:

    private:
    static Bar a; // OK
    Bar *b; // OK
    Bar c; // error

    My question is how a nonstatic member is declared as a reference to an
    object of its class. Because a reference is legal only after the
    original variable has been declared, where is the original object? I
    feel it is really bizarre. Could you give me an example? Thanks in
    advance.




    private variable
  • Jim Langston

    #2
    Re: Nonstatic member example?

    fl wrote:
    Hi,
    There is a question about nonstatic member. C++ primer says: A
    nonstatic member is restricted to being declared as a pointer or
    reference to an object of its class. It only gives an example of
    pointer *b.
    >
    class Bar {
    public:
    >
    private:
    static Bar a; // OK
    Bar *b; // OK
    Bar c; // error
    >
    My question is how a nonstatic member is declared as a reference to an
    object of its class. Because a reference is legal only after the
    original variable has been declared, where is the original object? I
    feel it is really bizarre. Could you give me an example? Thanks in
    advance.
    Passing it as a parameter to the constructor is one way. this (the instance
    pointer) is another. In fact, class member references have to be
    initialized in the constructor initialization list (I know of no other way)
    and passing as a paramter would be the usuall way. Something like (untested
    code)

    class Bar {
    public:
    Bar( Bar& foo ): d( foo ) {}
    private:
    Bar& d;
    };


    --
    Jim Langston
    tazmaster@rocke tmail.com


    Comment

    • Salt_Peter

      #3
      Re: Nonstatic member example?

      On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:
      Hi,
      There is a question about nonstatic member. C++ primer says: A
      nonstatic member is restricted to being declared as a pointer or
      reference to an object of its class. It only gives an example of
      pointer *b.
      >
      class Bar {
      public:
      >
      private:
      static Bar a; // OK
      Bar *b; // OK
      Bar c; // error
      >
      My question is how a nonstatic member is declared as a reference to an
      object of its class. Because a reference is legal only after the
      original variable has been declared, where is the original object? I
      feel it is really bizarre. Could you give me an example? Thanks in
      advance.
      >
      private variable
      the original object, in a special case like this one, would have to
      refer to itself, which would then basicly mean that such a class could
      not have a static member since the static member has no 'this'.
      Its a special case, don't dissmiss references. They solve many, many
      problems.

      [10.7] Should you use the this pointer in the constructor?


      #include <iostream>

      class A
      {
      const A& r_a;
      public:
      A() : r_a(*this) { }
      A(const A& copy) : r_a(copy) { }
      A& operator=(const A& rhv); // disabled
      A const& get_r() const { return r_a; }
      };

      void foo(const A& r)
      {
      std::cout << "&r = " << &r;
      std::cout << "\tr.r_a = " << &r.get_r();
      std::cout << std::endl;
      }

      int main()
      {
      A a;
      foo(a);
      A another = a; // is NOT an assignment
      foo(another);
      }

      /*
      &r = 0x7fff0f2e1930 r.r_a = 0x7fff0f2e1930
      &r = 0x7fff0f2e1920 r.r_a = 0x7fff0f2e1930
      */

      Comment

      • fl

        #4
        Re: Nonstatic member example?

        On 31 déc, 04:27, Salt_Peter <pj_h...@yahoo. comwrote:
        On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:
        >
        >
        >
        >
        >
        Hi,
        There is a question about nonstatic member. C++ primer says: A
        nonstatic member is restricted to being declared as a pointer or
        reference to an object of its class. It only gives an example of
        pointer *b.
        >
        class Bar {
        public:
        >
        private:
        static Bar a;      // OK
        Bar *b;              // OK
        Bar c;               // error
        >
        My question is how a nonstatic member is declared as a reference to an
        object of its class. Because a reference is legal only after the
        original variable has been declared, where is the original object? I
        feel it is really bizarre. Could you give me an example? Thanks in
        advance.
        >
        private variable
        >
        the original object, in a special case like this one, would have to
        refer to itself, which would then basicly mean that such a class could
        not have a static member since the static member has no 'this'.
        Its a special case, don't dissmiss references. They solve many, many
        problems.
        >
        [10.7] Should you use the this pointer in the constructor?http://www.parashift.com/c++-faq-lit....html#faq-10.7
        >
        #include <iostream>
        >
        class A
        {
          const A& r_a;
        public:
          A() : r_a(*this) { }
          A(const A& copy) : r_a(copy) { }
          A& operator=(const A& rhv); // disabled
          A const& get_r() const { return r_a; }
        >
        };
        >
        void foo(const A& r)
        {
          std::cout << "&r = " << &r;
          std::cout << "\tr.r_a = " << &r.get_r();
          std::cout << std::endl;
        >
        }
        >
        int main()
        {
          A a;
          foo(a);
          A another = a; // is NOT an assignment
          foo(another);
        >
        }
        >
        /*
        &r = 0x7fff0f2e1930     r.r_a = 0x7fff0f2e1930
        &r = 0x7fff0f2e1920     r.r_a = 0x7fff0f2e1930
        */- Masquer le texte des messages précédents -
        >
        - Afficher le texte des messages précédents -
        Hi,
        I find the modified code,see below, has the same output as yours.
        Why the overload:
        A another = a; // is NOT an assignment
        does not take effect? Thank you very much.





        ---------------------
        #include <iostream>


        class A
        {
        const A& r_a;
        public:
        A() : r_a(*this) { }
        // A(const A& copy) : r_a(copy) { }
        // A& operator=(const A& rhv); // disabled
        A const& get_r() const { return r_a; }



        };


        void foo(const A& r)
        {
        std::cout << "&r = " << &r;
        std::cout << "\tr.r_a = " << &r.get_r();
        std::cout << std::endl;


        }


        int main()
        {
        A a;
        foo(a);
        A another; // = a; // is NOT an assignment
        foo(another);


        }

        Comment

        • fl

          #5
          Re: Nonstatic member example?

          On 31 déc, 04:27, Salt_Peter <pj_h...@yahoo. comwrote:
          On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:
          >
          >
          >
          >
          >
          Hi,
          There is a question about nonstatic member. C++ primer says: A
          nonstatic member is restricted to being declared as a pointer or
          reference to an object of its class. It only gives an example of
          pointer *b.
          >
          class Bar {
          public:
          >
          private:
          static Bar a;      // OK
          Bar *b;              // OK
          Bar c;               // error
          >
          My question is how a nonstatic member is declared as a reference to an
          object of its class. Because a reference is legal only after the
          original variable has been declared, where is the original object? I
          feel it is really bizarre. Could you give me an example? Thanks in
          advance.
          >
          private variable
          >
          the original object, in a special case like this one, would have to
          refer to itself, which would then basicly mean that such a class could
          not have a static member since the static member has no 'this'.
          Its a special case, don't dissmiss references. They solve many, many
          problems.
          >
          [10.7] Should you use the this pointer in the constructor?http://www.parashift.com/c++-faq-lit....html#faq-10.7
          >
          #include <iostream>
          >
          class A
          {
            const A& r_a;
          public:
            A() : r_a(*this) { }
            A(const A& copy) : r_a(copy) { }
            A& operator=(const A& rhv); // disabled
            A const& get_r() const { return r_a; }
          >
          };
          >
          void foo(const A& r)
          {
            std::cout << "&r = " << &r;
            std::cout << "\tr.r_a = " << &r.get_r();
            std::cout << std::endl;
          >
          }
          >
          int main()
          {
            A a;
            foo(a);
            A another = a; // is NOT an assignment
            foo(another);
          >
          }
          >
          /*
          &r = 0x7fff0f2e1930     r.r_a = 0x7fff0f2e1930
          &r = 0x7fff0f2e1920     r.r_a = 0x7fff0f2e1930
          */- Masquer le texte des messages précédents -
          >
          - Afficher le texte des messages précédents -
          Sorry, my previous response is not right, i.e. Your's

          A another = a; // is NOT an assignment

          is right.
          My question now is: why there is no effect when I comment out:
          // A(const A& copy) : r_a(copy) { }
          // A& operator=(const A& rhv); // disabled

          Maybe the system does the copy in a special initializer fashion?
          I know now the object of "A" class is an address, which points to
          itself. That is from the following two lines. Right?
          const A& r_a;
          public:
          A() : r_a(*this) { }

          ---------------
          Then, what's the meaning of "another" after "another=a" ?
          I am not even clear:

          A(const A& copy) : r_a(copy) { }


          Thanks.

          Comment

          • Daniel T.

            #6
            Re: Nonstatic member example?

            fl <rxjwg98@gmail. comwrote:
            There is a question about nonstatic member. C++ primer says: A
            nonstatic member is restricted to being declared as a pointer or
            reference to an object of its class. It only gives an example of
            pointer *b.
            >
            class Bar {
            public:
            >
            private:
            static Bar a; // OK
            Bar *b; // OK
            Bar c; // error
            >
            My question is how a nonstatic member is declared as a reference to an
            object of its class. Because a reference is legal only after the
            original variable has been declared, where is the original object? I
            feel it is really bizarre. Could you give me an example? Thanks in
            advance.
            I have yet to see why one should have a non-static reference in a class
            in any case, no matter what it refers to.

            Comment

            • Salt_Peter

              #7
              Re: Nonstatic member example?

              On Dec 31, 10:35 am, fl <rxjw...@gmail. comwrote:
              On 31 déc, 04:27, Salt_Peter <pj_h...@yahoo. comwrote:
              >
              >
              >
              On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:
              >
              Hi,
              There is a question about nonstatic member. C++ primer says: A
              nonstatic member is restricted to being declared as a pointer or
              reference to an object of its class. It only gives an example of
              pointer *b.
              >
              class Bar {
              public:
              >
              private:
              static Bar a; // OK
              Bar *b; // OK
              Bar c; // error
              >
              My question is how a nonstatic member is declared as a reference to an
              object of its class. Because a reference is legal only after the
              original variable has been declared, where is the original object? I
              feel it is really bizarre. Could you give me an example? Thanks in
              advance.
              >
              private variable
              >
              the original object, in a special case like this one, would have to
              refer to itself, which would then basicly mean that such a class could
              not have a static member since the static member has no 'this'.
              Its a special case, don't dissmiss references. They solve many, many
              problems.
              >
              [10.7] Should you use the this pointer in the constructor?http://www.parashift.com/c++-faq-lit....html#faq-10.7
              >
              #include <iostream>
              >
              class A
              {
              const A& r_a;
              public:
              A() : r_a(*this) { }
              A(const A& copy) : r_a(copy) { }
              A& operator=(const A& rhv); // disabled
              A const& get_r() const { return r_a; }
              >
              };
              >
              void foo(const A& r)
              {
              std::cout << "&r = " << &r;
              std::cout << "\tr.r_a = " << &r.get_r();
              std::cout << std::endl;
              >
              }
              >
              int main()
              {
              A a;
              foo(a);
              A another = a; // is NOT an assignment
              foo(another);
              >
              }
              >
              /*
              &r = 0x7fff0f2e1930 r.r_a = 0x7fff0f2e1930
              &r = 0x7fff0f2e1920 r.r_a = 0x7fff0f2e1930
              */- Masquer le texte des messages précédents -
              >
              - Afficher le texte des messages précédents -
              >
              Sorry, my previous response is not right, i.e. Your's
              >
              A another = a; // is NOT an assignment
              >
              is right.
              My question now is: why there is no effect when I comment out:
              // A(const A& copy) : r_a(copy) { }
              // A& operator=(const A& rhv); // disabled
              the compiler generates the copy ctor if you don't. it probably does
              the exact same as the one commented out.
              I prefer declaring it in cases i want to diagnose problems and assert
              theories.

              A(const A& copy) : r_a(copy) { std::cout << "copy A"; }

              is nice to have when observing and troubleshooting .

              >
              Maybe the system does the copy in a special initializer fashion?
              not at all. Its a member-wise copy, nothing special.

              Its the same with something like
              struct K
              {
              int n;
              double d;
              char c;
              };

              You can initialize an instance of K and you can copy it because the
              compiler generates a copy ctor for you. It also generates a defult
              ctor and an assignment operator (if needed).
              I know now the object of "A" class is an address, which points to
              itself. That is from the following two lines. Right?
              const A& r_a;
              public:
              A() : r_a(*this) { }
              >
              ---------------
              Then, what's the meaning of "another" after "another=a" ?
              I am not even clear:
              >
              A(const A& copy) : r_a(copy) { }
              >
              Thanks.
              instance 'another' is a psuedo-copy of a. The difference is that
              another's member reference doesn't refer to another.
              This isn't something you need not worry about, you'll not find such a
              strategy in code out there.
              You do need to understand ctor, copy ctor and init list.

              Comment

              Working...