Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 19th, 2007, 01:15 PM
sachinc.biradar@gmail.com
Guest
 
Posts: n/a
Default 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

  #2  
Old September 19th, 2007, 01:35 PM
Barry
Guest
 
Posts: n/a
Default Re: Copy Constructor Query

sachinc.biradar@gmail.com wrote:
Quote:
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.
Quote:
knowledge referance is like const pointer.
you better think they are totally different on language level. on
implementation, I'm not sure.
Quote:
Any help will be highly appreciated.
>

--
Thanks
Barry
  #3  
Old September 19th, 2007, 01:55 PM
Neelesh Bodas
Guest
 
Posts: n/a
Default Re: Copy Constructor Query

On Sep 19, 5:09 pm, sachinc.bira...@gmail.com wrote:
Quote:
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
};


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

  #4  
Old September 19th, 2007, 02:05 PM
Barry
Guest
 
Posts: n/a
Default Re: Copy Constructor Query

Neelesh Bodas wrote:
Quote:
On Sep 19, 5:09 pm, sachinc.bira...@gmail.com wrote:
Quote:
>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
  #5  
Old September 19th, 2007, 02:25 PM
anon
Guest
 
Posts: n/a
Default Re: Copy Constructor Query

sachinc.biradar@gmail.com wrote:
Quote:
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.
  #6  
Old September 19th, 2007, 06:25 PM
Michal Nazarewicz
Guest
 
Posts: n/a
Default Re: Copy Constructor Query

sachinc.biradar@gmail.com writes:
Quote:
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?
Quote:
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--
  #7  
Old September 20th, 2007, 09:45 AM
James Kanze
Guest
 
Posts: n/a
Default Re: Copy Constructor Query

On Sep 19, 7:15 pm, Michal Nazarewicz <min...@tlen.plwrote:
Quote:
sachinc.bira...@gmail.com writes:
Quote:
Why can't we pass an argument to constructor as pointer?
Quote:
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:james.kanze@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

  #8  
Old September 20th, 2007, 10:35 PM
anupreet
Guest
 
Posts: n/a
Default Re: Copy Constructor Query

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

<sachinc.biradar@gmail.comwrote in message
news:1190203795.006444.303540@v29g2000prd.googlegr oups.com...
Quote:
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
>
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.