470,811 Members | 1,220 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

function signatures const vs. non const

Hello team,

C++ allows declarations of the fillowing form inside a class, which is
very confusing

void foo();
void foo() const;

My question is when will the second foo() be called as against first
foo()

Similarly

int bar();

const int bar();

Which function will be executed as a result of a call to bar() and
under what circumstances.

Thanks.

Apr 28 '06 #1
4 2516
arun wrote:
Hello team,

C++ allows declarations of the fillowing form inside a class, which is
very confusing

void foo();
void foo() const;

My question is when will the second foo() be called as against first
foo()

The const form will be called if the object is const, the non-const
otherwise.

--
Ian Collins.
Apr 28 '06 #2
arun wrote:
Similarly

int bar();

const int bar();

Which function will be executed as a result of a call to bar() and
under what circumstances.

I forgot this bit, you can't overload a const and non-const return with
the same function signature.

--
Ian Collins.
Apr 28 '06 #3
arun wrote:
Hello team,

C++ allows declarations of the fillowing form inside a class, which is
very confusing
Only if you don't know what the 'const' means in this place.
void foo();
void foo() const;

My question is when will the second foo() be called as against first
foo()
The second will be called if the object it is called for is const or
accessed through a reference or pointer to const. Otheriwse, the first one
is chosen.
Similarly

int bar();

const int bar();

Which function will be executed as a result of a call to bar() and
under what circumstances.


This shouldn't compile, because the functions have the same signature.

Apr 29 '06 #4
arun wrote:
Hello team,

C++ allows declarations of the fillowing form inside a class, which is
very confusing

void foo();
void foo() const;

My question is when will the second foo() be called as against first
foo()
class Circle
{
double radius;
public:
Circle(double r):radius(r){}

double& r(){return radius;}
const double& r() const{return radius;}
};

int main()
{
Circle c1(10);
const Circle c2(20);

// You should be able to read
// a const circle's radius:

double r1 = c1.r(); // calls Circle::r()
double r2 = c2.r(); // calls Circle::r() const

// But not to write to:

c1.r() = 3.5; // OK, writing to a double&
c2.r() = 3.5; // ERROR, cannot write to a const double&
}

Similarly

int bar();

const int bar();

Which function will be executed as a result of a call to bar() and
under what circumstances.
Under no circumstances because this won't compile.

Thanks.


Regards,
Ben
Apr 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by A. Saksena | last post: by
10 posts views Thread by sjbrown8 | last post: by
8 posts views Thread by Paul Tomlinson | last post: by
5 posts views Thread by krbyxtrm | last post: by
83 posts views Thread by Anonymous | last post: by
8 posts views Thread by minseokoh | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.