By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,951 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,951 IT Pros & Developers. It's quick & easy.

about consts and function overloading

P: n/a
ex1.foo(Person p);
foo(const Person p);

ex2.foo(Person *p);
foo(const Person *p);

ex3.foo(Person p);
foo(Person p) const;

why the pairs in ex1 are treated as the same function.Invalid
overloading. but ex2 is ok?

what is the meaning is ex3(foo(Person p)const;)?

thanks!
Nov 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Nick,
I'm afraid this batch of questions looks too much like homework or
something like that...
Searching the FAQs would provide you the answers.

"nick" <i1********@yahoo.com> wrote in message
news:dl***********@justice.itsc.cuhk.edu.hk...
: ex1.
: foo(Person p);
: foo(const Person p);

When a parameter is passed by value, the 'const' applies to the copy
of the object used within function foo.
Overloading will work as expected if you pass the parameter by
reference:
void foo( Person& p );
void foo( Person const& p );

: ex2.foo(Person *p);
: foo(const Person *p);
Note that the implementation of the first function could be written
as:
void foo( Person * const p ) { /* ... code ... */ };

: ex3.foo(Person p);
: foo(Person p) const;
:
: why the pairs in ex1 are treated as the same function.Invalid
: overloading. but ex2 is ok?
:
: what is the meaning is ex3(foo(Person p)const;)?
I'll leave this answer up to you.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact
form
Brainbench MVP for C++ <> http://www.brainbench.com
Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.