473,651 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if a method is overloaded with a const version, in the case thateither one is okay, which shall the compiler pick?

Dear all,

I have a question on the const methods. If a method is overloaded with
a const version, in the case that either one is okay (for example, the
following code), which shall the compiler pick? Could you tell which
section of the C++ standard specifies this?

Many thanks!

-Andy

#include <iostream>
using namespace std;

class A{
public:
int foo() const { return 6; }
int foo() { return 5; }
public:
int x;
};

int main()
{
A ca;
int c = ca.foo();
cout << "c = " << c << endl;
return 0;
}
Oct 26 '08 #1
2 1681
On 2008-10-26 14:53:09 -0400, yu******@gmail. com said:
I have a question on the const methods. If a method is overloaded with
a const version, in the case that either one is okay (for example, the
following code), which shall the compiler pick? Could you tell which
section of the C++ standard specifies this?
It's simply <ga matter of overload resolution.
#include <iostream>
using namespace std;

class A{
public:
int foo() const { return 6; }
int foo() { return 5; }
public:
int x;
};

int main()
{
A ca;
int c = ca.foo();
cout << "c = " << c << endl;
return 0;
}
The first version of A::foo takes a this pointer of type const A*; the
second one takes a this pointer of type A*. At the point of the call,
ca is not const, so the this pointer that the compiler generates has
type A*, and the call goes to the second version of A::foo. If that
version hadn't been defined, the call would go to the remaining (const)
version after a conversion of the type of the this pointer argument
from A* to const A*.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 26 '08 #2
On 2008-10-26 15:18:27 -0400, Pete Becker <pe**@versatile coding.comsaid:
On 2008-10-26 14:53:09 -0400, yu******@gmail. com said:
>I have a question on the const methods. If a method is overloaded with
a const version, in the case that either one is okay (for example, the
following code), which shall the compiler pick? Could you tell which
section of the C++ standard specifies this?

It's simply <ga matter of overload resolution.
>#include <iostream>
using namespace std;

class A{
public:
int foo() const { return 6; }
int foo() { return 5; }
public:
int x;
};

int main()
{
A ca;
int c = ca.foo();
cout << "c = " << c << endl;
return 0;
}

The first version of A::foo takes a this pointer of type const A*; the
second one takes a this pointer of type A*. At the point of the call,
ca is not const, so the this pointer that the compiler generates has
type A*, and the call goes to the second version of A::foo. If that
version hadn't been defined, the call would go to the remaining (const)
version after a conversion of the type of the this pointer argument
from A* to const A*.
Whoops, that's a bit ambiguous. The last sentence should start with "If
that version hadn't been declared," That is:

class A {
public:
int foo() const;
};

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 26 '08 #3

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

Similar topics

12
2315
by: Simula | last post by:
Hello All, Does anyone have any knowledge of when version 4 will be released? I think that version 3 was finalized in 1999 and it would be really nice to have the class keyword and statically typed variables. Thanks, Mark
16
41761
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the declaration of a const pointer? Is the following program legal C? int main(int argc, char *argv) {
6
8222
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the generic pointer type. My real question is, is (void *) such a generic pointer type that it
2
2885
by: Claudius | last post by:
Hello, I have written a class A with the access operator(int) overloaded by a A-const version which returns an int by value: ------------------------------------------------ #include <iostream> using namespace std;
10
2776
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1867
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
5
2290
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
9
5839
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
5
1671
by: __PPS__ | last post by:
Hello everybody, I'm not sure but I thought that sometime ago I had class with two overloaded methods: void func(const char*); and template<int N>func(const char (&arr)); so, that whenever I call func("some text") then size of the text will be known at compile time instead of doing strlen() I'm sure that long time ago I got it working somehow, but now when I needed and implemented a similar solution I see that it doesn't work.
0
8275
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8792
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8457
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7294
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
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.