473,606 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1689
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*******@gmai l.comwrote in message
news:11******** **************@ 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

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
2663
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 option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
3
1447
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 object, you must save the pointer of the object in super_auto_ptr //the class cannot work correctly in multithread
3
3400
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; foo() {} // no public creation!
6
2405
by: nutty | last post by:
Hi all, I have the following problem ( explanation below code ): // main.cpp class noncopyable { protected: noncopyable() {}
8
2394
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 (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
11
1906
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 destruction of the assigned memory seems to call the class destructor more than once. I don't know the reason or whether I used the vector class correctly. Attached is my program. Thanks for your help. Regards,
33
5040
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 actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
1
1565
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 how to initialize the
3
3897
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* foo; which a complicated code allocates as one of derived's and sets up.
0
8009
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7939
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8432
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8428
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8078
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6753
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.