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