473,327 Members | 2,012 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,327 software developers and data experts.

const vs const&

ek
This first example does not work (cannot be overloaded):

int& operator()(int a) { // (1)
return a;
}

int const& operator()(int a) { // (2)
return a;
}

but if I change (2) to (3):

int operator()(int a) const { // (3)
return a;
}

it works (overloading works). But why does (2) not work before I
change it to (3)?

May 16 '07 #1
2 2181

"ek" <ek*****@yahoo.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
This first example does not work (cannot be overloaded):

int& operator()(int a) { // (1)
return a;
}

int const& operator()(int a) { // (2)
return a;
}
Indeed, you can't overload based solely on return type. Besides, the both
yield undefined behaviour because you're returning a reference to a local
variable, but I know that's not your point ;)
but if I change (2) to (3):

int operator()(int a) const { // (3)
return a;
}

it works (overloading works)
Because (3) only works on a const *this, whereas (1) also works on a
non-const *this.

struct A
{
void func();
void func() const;
}

int main()
{
A a;
const A ca;

a.func(); // calls void A::func()
ca.func(); // calls void A::func() const
}

- Sylvester
May 16 '07 #2
On 5/16/2007 2:06 PM, ek wrote:
This first example does not work (cannot be overloaded):

int& operator()(int a) { // (1)
return a;
}

int const& operator()(int a) { // (2)
return a;
}
You can't overload on return type only. Make (2):

int const& operator()(int a) const {
return a;
}

but if I change (2) to (3):

int operator()(int a) const { // (3)
return a;
}

it works (overloading works). But why does (2) not work before I
change it to (3)?
S.
--
Stefan Naewe
stefan dot naewe at atlas-elektronik dot com
May 16 '07 #3

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

Similar topics

6
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
20
by: christopher diggins | last post by:
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance...
12
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
6
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to...
0
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it...
8
by: vsgdp | last post by:
Does this mean (T* const)& (i.e., a reference to a constant pointer to a T). So the pointer is constant but not the object pointed to?
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
2
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.