473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1232
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(con st 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 objektorientier ter Datenverarbeitu ng
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
3695
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? Right now I just use typedef vector<double>TS; Is that enough? I feel that I am not taking advantage of the C++
11
1751
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
2624
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, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
16
3101
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 has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
7
1833
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 xydata with operator- overloaded: class xydata { public:
110
4533
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
2302
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
3286
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, obj2 ;
8
2979
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, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
5
1341
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
9715
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10603
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...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
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
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6869
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
5536
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
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.