473,395 Members | 1,496 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,395 software developers and data experts.

overloding and const specifier

Hi group,

While trying out a piece of C++ code regarding oveloading,i noticed
the following.

class someclass
{
....
int func(int);
float func(int) const;
....
}

is legal. obviously without the const specifier, the compiler will
complain that overloading can not be done. that's fine.

my doubt is,
when does the const member function gets called???

i tried the following code (full) and noticed that all the while, the
first non-const member function was getting called.

******start of test.cpp******

#include <iostream>
using namespace std;

class A
{
private:
int i_data;
float f_data;

public:
A():i_data(0),f_data(0){};
A(int i,float f=0):i_data(i),f_data(f){};
int value(void);
float value(void) const;
};
inline int A::value()
{
cout<<"executing non-const value()\n";
return i_data;
}
inline float A::value() const
{
cout<<"executing const value()\n";
return f_data;
}

int main()
{
A objA(22,7.0);
int i=-1;
float f=-1.0;
i=objA.value();
cout<<"integer value = "<< i <<endl;
f=objA.value();
cout<<"float value = "<< f <<endl;
return 0;
}

******end of test.cpp******
output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
executing non-const value()
integer value = 22
executing non-const value()
float value = 22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm not sure if its an expected answer.

I understand that return type of a overloaded function does not matter
in resolving the invocations at the compiler level. Please correct me
if i'm wrong.

FYI,i compiled the program on the windows port of gcc version 3.2
(mingw special 20020817-1).

I am fairly new to C++ and i do not know in what real-world scenario
we might require such things. It is only my academic interest in
knowing how to invoke the const member function in the example sighted
above.

Thanks in Advance
-Kiran Shastry
Jul 19 '05 #1
1 2833

"Josephine Schafer" <js*@usa.net> wrote in message
news:bf************@ID-192448.news.uni-berlin.de...

"Kiran Shastry" <ki************@yahoo.co.in> wrote in message
news:69**************************@posting.google.c om...
Hi group,

While trying out a piece of C++ code regarding oveloading,i noticed
the following.

class someclass
{
...
int func(int);
float func(int) const;
...
}

is legal. obviously without the const specifier, the compiler will
complain that overloading can not be done. that's fine.

my doubt is,
when does the const member function gets called???
When you call it on a const object of the class.

i tried the following code (full) and noticed that all the while, the
first non-const member function was getting called.

******start of test.cpp******

#include <iostream>
using namespace std;

class A
{
private:
int i_data;
float f_data;

public:
A():i_data(0),f_data(0){};
A(int i,float f=0):i_data(i),f_data(f){};
int value(void);
float value(void) const;
};
inline int A::value()
{
cout<<"executing non-const value()\n";
return i_data;
}
inline float A::value() const
{
cout<<"executing const value()\n";
return f_data;
}

int main()
{
A objA(22,7.0);


Change it to const A objA(22,7.0);


Oh!..
thank for your immediate attention and this useful insight.
int i=-1;
float f=-1.0;
i=objA.value();
cout<<"integer value = "<< i <<endl;
f=objA.value();
cout<<"float value = "<< f <<endl;
return 0;
}

******end of test.cpp******
output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
executing non-const value()
integer value = 22
executing non-const value()
float value = 22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The output now is -
executing const value()
integer value = 7
executing const value()
float value = 7

the typing error in the subject line is regretted.
thanks again
-Kiran Shastry

--
With best wishes,
J.Schafer

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: William Payne | last post by:
Hello, in my application (which spans approximately 20 source files and a 2-3 thousand lines) I have a few global varialbes, variables that never change their values. One of them is declared like...
10
by: Dave | last post by:
const static int ARRAY_SIZE = 4; Comeau online gives this warning: "ComeauTest.c", line 10: warning: storage class is not first const static int ARRAY_SIZE = 4; Why is static const...
2
by: Tony Johansson | last post by:
Hello Experts! I have one class template here called Handle and one concrete class called Point. At the bottom is the main program and the class template definitions for Handle. This program...
3
by: Marcin Kalicinski | last post by:
void f(const char *&text); Is this a const reference to char * or a reference to const char *? And how to write both of them? thank you, Marcin
13
by: Amadeus W. M. | last post by:
I have a member static const int x defined in class Foo, and I'm passing it by reference, and by value elsewhere (see the code below). Passing it by value works, but by reference it doesn't: it...
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
27
by: Sundar | last post by:
Do we have something called a const function ? I thought applying the 'const' specifier to a function is meaningless... but when i tried defining a const function it worked !! like... const int...
5
by: huan | last post by:
int *p=new int(7) delete p; could be written as void* buf=operator new(sizeof(int)); int* p=new (buf) int(7); operator delete(p); My question is how to use operator new, placement new to...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.