473,407 Members | 2,629 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,407 software developers and data experts.

Copy Constructor Query

Hi,
I am pretty new to C++, I have following doubt.
Why can't we pass an argument to constructor as pointer? As per my
knowledge referance is like const pointer.
Any help will be highly appreciated.

Regards,
Sachin

Sep 19 '07 #1
7 2085
sa*************@gmail.com wrote:
Hi,
I am pretty new to C++, I have following doubt.
Why can't we pass an argument to constructor as pointer? As per my
that's a language design issue. better discuss on c.std.c++ or read
Bjarne's D&E, I haven't read that book.
knowledge referance is like const pointer.
you better think they are totally different on language level. on
implementation, I'm not sure.
Any help will be highly appreciated.

--
Thanks
Barry
Sep 19 '07 #2
On Sep 19, 5:09 pm, sachinc.bira...@gmail.com wrote:
Hi,
I am pretty new to C++, I have following doubt.
Why can't we pass an argument to constructor as pointer?
You can, by all means. The only point to note is that it won't be
called a copy constructor.

struct X
{
X(X*); //fine, a constructor
};

It won't act as a copy constructor because - according to the C++
standard the copy constructor must take a reference to the same class:

struct X
{
X(X&); // copy constructor
};
>As per my knowledge referance is like const pointer.
A reference is a reference, and a const pointer is a const pointer.
They are different constructs, and neither of them is necessarily
"like" any other.

-Neelesh

Sep 19 '07 #3
Neelesh Bodas wrote:
On Sep 19, 5:09 pm, sachinc.bira...@gmail.com wrote:
>Hi,
I am pretty new to C++, I have following doubt.
Why can't we pass an argument to constructor as pointer?

You can, by all means. The only point to note is that it won't be
called a copy constructor.

struct X
{
X(X*); //fine, a constructor
};
but the OP stated Copy Constructor in his subject.
though it didn't mention this in the content.

--
Thanks
Barry
Sep 19 '07 #4
sa*************@gmail.com wrote:
I am pretty new to C++, I have following doubt.
Why can't we pass an argument to constructor as pointer? As per my
knowledge referance is like const pointer.
Any help will be highly appreciated.
You can pass NULL to a pointer. You can not do that to a reference.
Sep 19 '07 #5
sa*************@gmail.com writes:
Why can't we pass an argument to constructor as pointer?
Because this would allow passing NULL to copy constructor and what does
it mean to copy object which does not exist?
As per my knowledge referance is like const pointer.
On implementation level probably yes, however:
1. const pointer can have NULL value (ie. point to nowhere), reference
always references some object;
2. you can use pointer arithmetic with const pointer but you cannot do it
with reference; and
3. you cannot have reference to void type.

For instance:

#v+
void funcPtr(char *const ptr) {
std::cout << *ptr << '\n'; /* may produce undefined behaviour because
ptr may be NULL. */
for (int i = 0; i < 10; ++i) {
std::cout << ptr[i] << '\n'; /* you can treat ptr as array. */
}
}

void funcRef(char &ref) {
std::cout << ref << '\n'; /* this won't be UB. */
for (int i = 0; i < 10; ++i) {
std::cout << ref[i] << '\n'; /* error: you cannot do such a thing */
}
}
#v-
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 19 '07 #6
On Sep 19, 7:15 pm, Michal Nazarewicz <min...@tlen.plwrote:
sachinc.bira...@gmail.com writes:
Why can't we pass an argument to constructor as pointer?
Because this would allow passing NULL to copy constructor and what does
it mean to copy object which does not exist?
It would also mean that you couldn't pass a temporary.

Most significant, however, is that the results wouldn't have the
correct syntax. To copy, you write:
A a( anotherA ) ;
and not
A a( &anotherA ) ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 20 '07 #7
Copy constructor is yet another means of "creating" an object. Reference
parameters are used directly, they are not copied into a local variable like
when they are passed by value or pointers.
Passing a pointer of the object to a copy constructor will lead to invoking
a copy constructor for the passed object....and so the trouble.

Rest you can pass any type (pointer, reference) of all data types to a
constructor.

-AW

<sa*************@gmail.comwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...
Hi,
I am pretty new to C++, I have following doubt.
Why can't we pass an argument to constructor as pointer? As per my
knowledge referance is like const pointer.
Any help will be highly appreciated.

Regards,
Sachin
Sep 20 '07 #8

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
1
by: Vivek Shah | last post by:
Hi, Given below is apiece of code which I was writing to clear my concepts of copy constructor. I have a function f() which takes Class A object through call by value and return the same object....
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.