472,378 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 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 2074

"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: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.