about consts and function overloading

N

nick

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

Ivan Vecerina

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.

: 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
 

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
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top