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

constant member functions

Hi,
Can a constant member function be overloaded with its non-constant
equivalent. I mean can void func() be overloaded with void func()
const. If so what behaviour is guaranteed, how does the compiler
resolve ambiguity?, and how could I call a specific variant?

tia,
Srinivas
Jul 19 '05 #1
2 6502
"srinivas reddy" <sr*************@yahoo.com> wrote...
Can a constant member function be overloaded with its non-constant
equivalent.
If that's a question (somehow I got used to seeing '?' at
the ends of questions), then the aswer is 'yes'.
I mean can void func() be overloaded with void func()
const.
Yes.
If so what behaviour is guaranteed, how does the compiler
resolve ambiguity?
There is no ambiguity. Non-const function is preferred when
called for a non-const object. For a const object non-const
function cannot be called.
, and how could I call a specific variant?


By using a cons_cast if the const-ness of the function is
not to your satisfaction. However, casting away const and
then calling a non-const function for a const object will
result in undefined behaviour.

Victor
Jul 19 '05 #2
srinivas reddy wrote:
...
Can a constant member function be overloaded with its non-constant
equivalent. I mean can void func() be overloaded with void func()
const.
Yes.
If so what behaviour is guaranteed, how does the compiler
resolve ambiguity?, and how could I call a specific variant?
...


For overloading purposes, you can think of member functions as if they
are ordinary functions with an additional implicit 'this' parameter.
When declared as a member of class 'A', method 'void func()' is
equivalent to standalone function 'void func(A* this)' and method 'void
func() const' is equivalent to standalone function 'void func(const A*
this)'. The overloading will work the same way it works for standalone
functions. For non-constant object the first version is called, for
constant objects the second version is called.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #3

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

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
4
by: Fraser Ross | last post by:
Are these pointers the same? Fraser.
3
by: blueblueblue2005 | last post by:
Hi, what is the different between the following two ways to use const in a function definition: 1. const Array &operator=(const Array &); 2. int getSize() const; keyword const to the left of...
10
by: Dennis Jones | last post by:
Hello, I have a hierarchy of classes in which there will be a data element that is common to all descendant classes. This element (a string in this case) is constant, but specific to each...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
13
by: Anarki | last post by:
#include <iostream> using namespace std; class ConstantMember { const int m_const; public: ConstantMember(int cons = 10):m_const(cons){} int getConst() const{ return m_const;} };
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
8
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n),...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.