C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

overloading operator []

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Aff@n
Guest





PostPosted: Mon Aug 28, 2006 9:10 am    Post subject: overloading operator [] Reply with quote



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;
};
Back to top
Kai-Uwe Bux
Guest





PostPosted: Mon Aug 28, 2006 9:10 am    Post subject: Re: overloading operator [] Reply with quote



Aff@n wrote:

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.