473,399 Members | 3,603 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,399 software developers and data experts.

overloading operator []

Aff
hi,
i wold like to ask, why is it when overloading operator[] we send int
value is there any way around?.
e.g.
class abc
{
protected:
int *a;
private:
int operator[](int );
};

int abc::operator[](int x)
{
int z;
z=a[x];
return z;
};

Aug 28 '06 #1
3 2037
Aff@n wrote:
hi,
i wold like to ask, why is it when overloading operator[] we send int
value is there any way around?.
e.g.
class abc
{
protected:
int *a;
private:
int operator[](int );
};

int abc::operator[](int x)
{
int z;
z=a[x];
return z;
};
In your case, operator[] takes an int argument because and only because you
defined it that way. You could as well do:

class abc
{
protected:
int *a;
private:
int operator[]( std::size_t );
};

int abc::operator[]( std::size_t x)
{
int z;
z=a[x];
return z;
};
Best

Kai-Uwe Bux
Aug 28 '06 #2
>Aff@n wrote:
>
>hi,
i wold like to ask, why is it when overloading operator[] we send int
value is there any way around?.
Sure there is. Consider std::map<std::string, int>.
Its operator[] takes std::string for an argument.

Aug 28 '06 #3

"Aff@n" <af********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
hi,
i wold like to ask, why is it when overloading operator[] we send int
value is there any way around?.
e.g.
class abc
{
protected:
int *a;
private:
int operator[](int );
here you are telling it to take an int value. You want it to take something
else, have it.

int operator[]( const std::string& );
int operator[]( float );
int operator[]( const MyClass& );
};

int abc::operator[](int x)
{
int z;
z=a[x];
return z;
};
Of course, you'd want to do somethign meaningful with the parameter. In one
of my classes I am passing a std::string& which I am using to look up the
value in a map with std::string as the key.

Proxy operator[]( const std::string& Key )
{
return Proxy(*this, Key);
}

Of coure I'm not returning an int either, but my own class. Why I'm doing
this really has nothing to do with your question though, this is just to
show you that operator[] can accept and return anything.
Aug 29 '06 #4

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

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
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...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
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,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
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,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
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...
0
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...
0
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,...
0
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...

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.