Experimental only: Pointer copy consturctor does not work

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

    #1

    Experimental only: Pointer copy consturctor does not work

    Hello group,

    Last week I picked up a thread, which pointed out that if a copy
    constructor is created with pointers
    instead of reference, there is a danger of it going in infinite
    recursion.

    My observation:

    1. Compiler does not complain.
    2. Does not go in infinite recursion.
    3. Does not work the way it should have worked.

    Following is my code.

    using namespace std;
    #include <iostream>

    class B {

    public:

    B() {cout << "inside B default" << endl;}

    B(B* cp) {cout << "inside B pointer copy" << endl;}

    };


    int main(void) {

    B* b = new B;
    B* some = b;
    return 0;

    }

    The program should print

    inside B default
    inside B pointer copy

    Instead it prints only

    inside B default.

    Any ideas, comments.

    Thanks.

    nagrik

  • Victor Bazarov

    #2
    Re: Experimental only: Pointer copy consturctor does not work

    nagrik wrote:
    Hello group,
    >
    Last week I picked up a thread, which pointed out that if a copy
    constructor is created with pointers
    instead of reference, there is a danger of it going in infinite
    recursion.
    >
    My observation:
    >
    1. Compiler does not complain.
    2. Does not go in infinite recursion.
    3. Does not work the way it should have worked.
    >
    Following is my code.
    >
    using namespace std;
    #include <iostream>
    >
    class B {
    >
    public:
    >
    B() {cout << "inside B default" << endl;}
    >
    B(B* cp) {cout << "inside B pointer copy" << endl;}
    >
    };
    >
    >
    int main(void) {
    >
    B* b = new B;
    B* some = b;
    return 0;
    >
    }
    >
    The program should print
    >
    inside B default
    inside B pointer copy
    >
    Instead it prints only
    >
    inside B default.
    >
    Any ideas, comments.
    Did you mean to do

    B* b = new B;
    B some = b;

    ??? Otherwise, you're not constructing another B object, only
    another pointer to the same B object...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Howard

      #3
      Re: Experimental only: Pointer copy consturctor does not work


      "nagrik" <jainarunk@gmai l.comwrote in message
      news:1161722321 .472720.146530@ f16g2000cwb.goo glegroups.com.. .
      Hello group,
      >
      Last week I picked up a thread, which pointed out that if a copy
      constructor is created with pointers
      instead of reference, there is a danger of it going in infinite
      recursion.
      >
      My observation:
      >
      1. Compiler does not complain.
      2. Does not go in infinite recursion.
      3. Does not work the way it should have worked.
      >
      Following is my code.
      >
      using namespace std;
      #include <iostream>
      >
      class B {
      >
      public:
      >
      B() {cout << "inside B default" << endl;}
      >
      B(B* cp) {cout << "inside B pointer copy" << endl;}
      This is not a valid copy constructor. In order for this to work as a copy
      constructor, you'd need to modify the compiler to recognize it as one, and
      use it when copy-constructing.

      The compiler is going to generate a copy constructor for you, since you
      haven't provided one here.
      >
      };
      >
      >
      int main(void) {
      >
      B* b = new B;
      B* some = b;
      This would never even call the copy constructor (even if the above were an
      acceptable form for one). It's merely copying a pointer value.
      return 0;
      >
      }
      >
      The program should print
      >
      inside B default
      inside B pointer copy
      >
      Instead it prints only
      >
      inside B default.
      >
      Any ideas, comments.
      The program is correct. :-)

      -Howard





      Comment

      • Clark S. Cox III

        #4
        Re: Experimental only: Pointer copy consturctor does not work

        nagrik wrote:
        Hello group,
        >
        Last week I picked up a thread, which pointed out that if a copy
        constructor is created with pointers
        Copy constructors are not created with pointers, so the issue is moot.
        instead of reference, there is a danger of it going in infinite
        recursion.
        >
        My observation:
        >
        1. Compiler does not complain.
        2. Does not go in infinite recursion.
        3. Does not work the way it should have worked.
        >
        Following is my code.
        >
        using namespace std;
        #include <iostream>
        >
        class B {
        >
        public:
        >
        B() {cout << "inside B default" << endl;}
        >
        B(B* cp) {cout << "inside B pointer copy" << endl;}
        This is not a copy constructor, it is a constructor that takes a pointer
        >
        };
        >
        >
        int main(void) {
        >
        B* b = new B;
        B* some = b;
        return 0;
        >
        }
        >
        The program should print
        No, it shouldn't.
        >
        inside B default
        inside B pointer copy
        >
        Instead it prints only
        >
        inside B default.
        >
        Any ideas, comments.
        You're only constructing a single B object (created with "new B"), you
        are then setting two pointer variables ("b" and "some") to point to it.

        Perhaps, you wanted something like:


        int main(void) {

        B* b = new B;
        B some = b; /*some is not a pointer*/
        return 0;
        }

        --
        Clark S. Cox III
        clarkcox3@gmail .com

        Comment

        • Salt_Peter

          #5
          Re: Experimental only: Pointer copy consturctor does not work


          nagrik wrote:
          Hello group,
          >
          Last week I picked up a thread, which pointed out that if a copy
          constructor is created with pointers
          instead of reference, there is a danger of it going in infinite
          recursion.
          Thats not a danger since a ctor with a pointer is not a copy ctor, its
          a parametized ctor.
          A pointer is not an object. A reference, however, is an object.
          A pointer is just an address, it may or may not point to anything.
          >
          My observation:
          >
          1. Compiler does not complain.
          2. Does not go in infinite recursion.
          3. Does not work the way it should have worked.
          >
          Following is my code.
          >
          using namespace std;
          #include <iostream>
          >
          class B {
          >
          public:
          >
          B() {cout << "inside B default" << endl;}
          >
          B(B* cp) {cout << "inside B pointer copy" << endl;}
          Thats not a copy ctor. see below.
          >
          };
          >
          >
          int main(void) {
          >
          B* b = new B;
          B* some = b;
          return 0;
          >
          }
          >
          The program should print
          >
          inside B default
          inside B pointer copy
          How? All i see is a ctor + a pointer being copied.
          >
          Instead it prints only
          >
          inside B default.
          >
          Any ideas, comments.
          You confusion is partly because you aren't labelling your pointers
          appropriately. The "variable" b above is not an instance of B, its just
          a dumb pointer. The same goes for some.

          The first line in main() invokes a default ctor.
          Nowhere else is construction of any kind taking place.
          Copying a pointer does not invoke an object ctor of any kind.

          Now consider the class below. It has:
          a) a default ctor
          b) a parametized ctor - which doesn't do anything usefull with the
          parameter
          c) a copy ctor
          d) a d~tor

          Follow the output.

          #include <iostream>

          class B
          {
          public:
          B() {std::cout << "B()\n";}
          B(B* p) {std::cout << "B(B* p)\n";}
          B(const B& copy) {std::cout << "copy B()\n";}
          ~B() {std::cout << "~B()\n";}
          };

          int main()
          {
          B* p(new B); // exactly the same as B* p = new B, invoking the
          default ctor
          B another(*p); // what is *at* p is being passed, hence a copy!
          // p is a pointer, *p is not a pointer
          B instance(p); // invoking a parametized ctor B( B* )

          B* p_b = &instance; // nothing happened, a dumb pointer gets an
          address
          B* p_b2 = p_b; // same
          B* p_b3 = p_b2; // same

          delete p; // required
          return 0;
          }

          /*
          B()
          copy B()
          B(B* p)
          ~B()
          ~B()
          ~B()
          */

          Comment

          • Michiel.Salters@tomtom.com

            #6
            Re: Experimental only: Pointer copy consturctor does not work

            Salt_Peter wrote:
            nagrik wrote:
            Hello group,

            Last week I picked up a thread, which pointed out that if a copy
            constructor is created with pointers
            instead of reference, there is a danger of it going in infinite
            recursion.
            >
            Thats not a danger since a ctor with a pointer is not a copy ctor, its
            a parametized ctor.
            A pointer is not an object. A reference, however, is an object.
            No, you got it flipped. References aren't objects, pointers are.
            However,
            pointers are not objects of class type. You can't define copy
            constructors
            for pointers, just like you can't define the copy ctor for int. Hence,
            pointers
            can be memcpy'd safely etcetera.

            (Ignoring smart pointers like std::auto_ptr<- that's a class
            template)

            Regards,
            Michiel Salters

            Comment

            Working...