473,320 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Oct 24 '06 #1
5 1672
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
Oct 24 '06 #2

"nagrik" <ja*******@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.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

Oct 24 '06 #3
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
cl*******@gmail.com
Oct 24 '06 #4

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()
*/

Oct 24 '06 #5
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

Oct 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
3
by: Zheng Da | last post by:
Will the following class work well? If it can not work correctly in some situation, could you help me to fix it? Thank you. //the class will work like the reference in java //when you create a...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
6
by: nutty | last post by:
Hi all, I have the following problem ( explanation below code ): // main.cpp class noncopyable { protected: noncopyable() {}
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
1
by: brekehan | last post by:
My C++ has gotten so rusty!!! If my constructor looks like this: Image2D::Image2D(LPDIRECT3DDEVICE9 device) : SceneObject2D(device), <snip> How do I do my copy constructor...I am not sure...
3
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base*...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.