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

Question about operator overloading

In this case, how does the compiler knows which to run, given
that both operators receive the same parameter ?
class String
{
// overloaded operators
char & operator[](int offset);
char operator[](int offset) const;

//Which one gets runed ? If not based on parameters then based on
what ?
};

Thanks
Jun 27 '08 #1
6 1209
Rafael Anschau wrote:
In this case, how does the compiler knows which to run, given
that both operators receive the same parameter ?
class String
{
// overloaded operators
char & operator[](int offset);
char operator[](int offset) const;

//Which one gets runed ? If not based on parameters then based on
what ?
In the expression str[index], index is a visible parameter to operator[],
but str also enters the picture. If str is a const object, then the const
member function will be chosen; and if str is non-const, the non-const
member function will be chosen.
Best

Kai-Uwe Bux
Jun 27 '08 #2
On May 15, 5:35*pm, Rafael Anschau <rafael.ansc...@gmail.comwrote:
In this case, how does the compiler knows which to run, given
that both operators receive the same parameter ?

class String
*{
* // overloaded operators
*char & operator[](int offset);
*char operator[](int offset) const;

//Which one gets runed ? If not based on parameters then based on
what ?

};
Hi.

If you apply the operator[] on a constant String, the compiler will
automatically select the const version of it. This could happen, for
example, in a function like the one below:

void doSomething(const String& s)
{
//Here, the const version of operator[] would be chosen.
}

As an additional note, you could write the const version of your
operator like this:

class String
{
//...
const char& operator[](int offset) const;
};
--
Leandro T. C. Melo

Jun 27 '08 #3
Interesting, the only semantic description I have for const(after
a method) is that it doesn´t change the object´s member data. Are
there other places where const may have other meanings(aside from data
types
like "const int a=10;" etc, and operators) ?

Thanks,

Rafael
Jun 27 '08 #4
On May 16, 4:35*am, Rafael Anschau <rafael.ansc...@gmail.comwrote:
In this case, how does the compiler knows which to run, given
that both operators receive the same parameter ?

class String
*{
* // overloaded operators
*char & operator[](int offset);
*char operator[](int offset) const;

//Which one gets runed ? If not based on parameters then based on
what ?

};

Thanks

The compiler will do the corrent thing for you.
Augument liks this :
String s("aa");
char c = s[0]; // call char operator[](int offset) const;
s[0] = 'c'; // call char & operator[](int offset);

That is the difference you want.

Jun 27 '08 #5
lunar_lty wrote:
On May 16, 4:35 am, Rafael Anschau <rafael.ansc...@gmail.comwrote:
>In this case, how does the compiler knows which to run, given
that both operators receive the same parameter ?

class String
{
// overloaded operators
char & operator[](int offset);
char operator[](int offset) const;

//Which one gets runed ? If not based on parameters then based on
what ?

};

Thanks


The compiler will do the corrent thing for you.
Augument liks this :
String s("aa");
char c = s[0]; // call char operator[](int offset) const;
s[0] = 'c'; // call char & operator[](int offset);

That is the difference you want.
Actually they both call the non-const version in your example. Consider
the following analogous program:

#include <iostream>

struct S
{
char c;

S(char c_) : c(c_) {}

char& operator()()
{
std::cout << "non-const" << std::endl;
return c;
}

char operator()() const
{
std::cout << "const" << std::endl;
return c;
}
};

int main()
{
S s('a');
char c = s();
s() = 'b';
const S& cs = s;
std::cout << cs() << std::endl;

return 0;
}

This prints:

non-const
non-const
const
b

In other words, the first call is to the non-const operator() - not the
const one as you suggested. It's the constness (or otherwise) of the
instance of S which is determining which method/operator gets called. In
the case of cs, it's a reference to a const S, so only const methods of
S can be called on it.

Regards,
Stu
Jun 27 '08 #6
On 16 mai, 16:17, Rafael Anschau <rafael.ansc...@gmail.comwrote:
Interesting, the only semantic description I have for const(after
a method) is that it doesn´t change the object´s member data. Are
there other places where const may have other meanings(aside from data
types like "const int a=10;" etc, and operators) ?
const is part of the type system, and plays a defined role
anywhere types are significant.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7

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

Similar topics

7
by: Jessica | last post by:
Hi, I have a design question. I am making a time series analysis tool. Since I already use STL vector to represent time series, is there a need to implement a class for the time series object? ...
11
by: billnospam | last post by:
Is it possible to overload operators in vb.net? Is it possible to do programmer defined boxing on byvalue variables in vb.net?
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
110
by: Vijay Kumar R Zanvar | last post by:
Hi, Which section of C99 says that return value of malloc(3) should not be casted? Thanks. -- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
5
by: Joseph Y. Oh | last post by:
Hello, class A { public: A& operator+=( const A& a ) = 0; }; class B1:public A { public: A& operator+=( const A& a ) { ... }
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.