473,586 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6518
"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
4823
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 to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I...
4
1938
by: Fraser Ross | last post by:
Are these pointers the same? Fraser.
3
1492
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 return type and to the right of the parameter list, what is the different here? I know for the 2nd case, the function can not modify the object's...
10
1588
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 class. So I have something like this: class Base { private: virtual std::string GetString() const = 0;
3
20849
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 attempts so far have been unsuccessful. Take this Test class as an example // test.hpp
13
1632
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
2801
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 naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. ...
8
1660
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), and the information to assign (a const pointer to a class object Vector<TYPE>. According to the arrow below, I put a const keyword in the...
7
4255
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 expression") or Comeau Online ("constant value is not known"). If I replace
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.