Experimental only: Pointer copy consturctor does not work

N

nagrik

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
 
V

Victor Bazarov

nagrik said:
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
 
H

Howard

nagrik said:
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
 
C

Clark S. Cox III

nagrik said:
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;
}
 
S

Salt_Peter

nagrik said:
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()
*/
 
M

Michiel.Salters

Salt_Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top