473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2655
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
1
by: A. Saksena | last post by:
Hi all, I am trying to compile following code :- ======================================= #include <iostream.h> #include <string> namespace tlm { template <typename REQUEST, typename RESPONSE >...
10
by: sjbrown8 | last post by:
I have the piece of code below, and when i try compiling with the line g++ 753075304.cpp I get the following error message: 753075304.cpp: In function 'int main()': 753075304.cpp:29: error:...
8
by: Paul Tomlinson | last post by:
All, i'm at my wits-end. I have this third party C++ function which I am trying to call from C#, no matter what I try to do I just can't get it to work. This is the function prototype in C++: ...
8
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in...
5
by: krbyxtrm | last post by:
Any knows of a Function Signature string Parser? typically one that can parse string: "unsigned int const * __cdecl MyFunction(int,void const *)" into: 0 : 'unsigned int const *' 1 :...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.