Copy Constructor Query

S

sachinc.biradar

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
 
B

Barry

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

Neelesh Bodas

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
 
B

Barry

Neelesh said:
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.
 
A

anon

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

Neelesh Bodas

You can pass NULL to a pointer. You can not do that to a reference.

Incorrect. You can't "pass" anything to a pointer or a reference.
entities can be "passed" only to functions. (Perhaps you meant
"assign".)

-N
 
A

anon

Neelesh said:
Incorrect. You can't "pass" anything to a pointer or a reference.
entities can be "passed" only to functions. (Perhaps you meant
"assign".)

No, I meant pass. As in this example:

void f( const int *p )
{
}
void g( const int &i )
{
}
int main()
{
f(NULL);
g(5);
}

Maybe I misunderstood OP's question.
 
G

Guest

No, I meant pass. As in this example:

void f( const int *p )
{
}
void g( const int &i )
{
}
int main()
{
f(NULL);
g(5);
}

Maybe I misunderstood OP's question.

No, you are just having some problems with the terminology, in the above
example you pass a NULL pointer to the function f(), you do not pass
NULL to a pointer.
 
M

Michal Nazarewicz

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 << '\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 << '\n'; /* error: you cannot do such a thing */
}
}
#v-
 
J

James Kanze

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 ) ;
 
A

anupreet

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
 

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
474,274
Messages
2,571,370
Members
48,062
Latest member
leehaan

Latest Threads

Top