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
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