I have few question, If i have two private member array in my class how can I use [] operator for my use, something like following,

class foo
{
         int iarray[10];
         char carray[10]
    public:
           //constructors        
           //copy cons nd assignment ans all

};

So my first question is When we have both in and char array and we cant overload [] operator, so whats the way to do use [] operator when we have more than two arrays.

My second problem is

#include<iostream>
#include<string.h>
using namespace std;
class base
{
        private:
                char* name;
                char snaime[10];
           public:
                base();
                ~base();
                base(const char*);
                base(const base&);
                void display(void);
                base operator= (const base&);
                friend base add(base,base);
                 friend ostream& operator<<(ostream&, base&);
                friend istream& operator>>(istream&, base&);
                char& operator[](const int);
 
};

base::base()
{
         name = new char(0);
}

base::~base()
{
        delete name;
}

base::base(const char* foo)
{
        name = new char[strlen(foo)+1];
        strcpy(name,foo);
}

base::base(const base& foo)
{
        name = new char[strlen(foo.name)+1];
        strcpy(name, foo.name);
}
base base::operator=(const base& rhs)
{
        cout<<"Inside assignmnet .."<<endl;

        if(this== &rhs)
        {
                cout<<"Same assignment creepo.."<<endl;
                return *this;
        }

        delete name;
        name =  new char[strlen(rhs.name)+1];
        strcpy(name,rhs.name);


        return *this;
}

ostream& operator<<(ostream& outt, base& b1)
{
        outt<<"Name chip: "<<b1.name<<endl;
        return outt;
}

istream& operator>>(istream& inn, base& b1)
{
        cout<<"Enter the name"<<endl;
        inn>>b1.name;
        return inn ;
}

char& base::operator[](const int index)
{
        return sname[index];
}

int main()
{

base b1;
b1[0]="Gladiator";

cout<<b1[0]<<endl;

       return 0;
}

Its giving me some "invalid conversion from ‘const char*’ to ‘char’
" ERROR .. So how to get rid of this.. Where I am doing wrong.

thanx in advance

1. The 1st problem is undefined. Please, explain what you need: do you want to access private member arrays from the outer world? If so, why you declare these arrays as private? I can't understand your (sub)question "how can I use [] operator for my use, something like following,". No operator[] using in the following snippet. Well, declare next member functions to access different internal arrays. But you can define the only operator [] for your class - it's a class operator, not a member operator.

2. There are some errors in your base class. Let's begin from the error message. You declare name member as a pointer to char. The base::operator[] returns a reference to char. Now you try to assign a const pointer to char (it's a type of a string literal in C++) to char (via this reference) . Of course, it's impossible. Is it a great surprise to you?
3. Some other defects in the base class:
3.1 Try to avoid all-small-letters names for your classes. It's a common practice to assign names started from a capital letter (avoid permissible but undesired clashes with RTL names).
3.2 Right signature for overloaded operator <<:

ostream& operator<<(ostream& outt, const base& b1) // add const

3.3 Correct obvious misprint (snaime). Now your operator[] refers to inexistent name.

Thanx buddy I solved my silly mistake (The second problem).

Well My first question is we can overload subscript operator when we have oneD array in our class. But If we have more than two OneD array in our class so how can we use subscript operator for both the array.

AS I know for direct access of oneD array we overload [] operator, so what could be done in the case of two oneD array in the class.

like

class Base
{
      private:
                  int iArray[10];
                  char cArray[10];
     public:
                 Base();
                 // other stuff
                int& operator[](const int index);      


};

So here I am overloading [] for int iArray , My question is how can we overload [] for cArray[] now, as it says double overload is not possible ?

It seems you don't understand: you can't "overload subscript operator for cArray"! It's impossible to overload operators for basic types. You overload subscript operator for YOUR CLASS. So your question is like:
- How overload already overloaded subscript operator for my class?
Well if you want: overload operator() for your class. Let it get full access to the 2nd array elements (in Fortran subscript style ;)). I think it's not the best solution.

Again and again: you can add member functions to access the 2nd member array in your class. What's a problem?

I believe you don't understand exactly process of "overloading" operators.
You can really type whatever you want to do. It's just like a function. If you wish, it can print stuff on the screen.
So the question is: what do you want it to do?

Yet better, how do you want to use it?

To add to the discussion, here is a bit more about Operator Overloading.

For example lets take a part of an ArrayClass where you overload the operator [] for objects of this class

class Array{
  ----
public:
  int& operator[](int); /* pass in subscript */
  ----
 private:
    int        ArraySize;
    int*       ArrayPtr;
};

int& Array::operator[](int subscript){

  assert(0 <= subscript && subscript < ArraySize);

  return ArrayPtr[subscript];
}

Now in your main program you have

int main{
  Array  ArrayObject;
  
  // use the overloaded operator [] on ArrayObject
  ArrayObject[5] = 10;   
  ArrayObject.operator [](5) = 10;
 
}

As you can see the overloaded operator [] is translated as a call to the function operator[] for this array object.

So you really need to get your fundamentals straight.

Sorry as my question was vague,

Look the thing is

class Base
{
      private:
                  int iArray[10];
                  char* cArray[10];
     public:
                 Base();
                 // other stuff
                int& operator[](const int index);      
                char*& operator[](const int index);

};

int& base::operator[](const int index)
{
        return ifoo[index];
}

char*& base::operator[](const int index)
{
        return cArray[index];
}

Here it gives me error you cant overload [] twice.
Hope you get my exact problem.

When my class has two arrays of different type how can we overload [] for each type array.

thanx

Of course you cannot overload operator[] in that way!
Since it has same type of argument, program won't know to which one are you reffering (sice it doesn't check type that you use to hold return value)

Answer: You CAN'T do that

So whats the way If I encounter that situation ? Thats exactly my question is !!!

Well I presume you could make one operator, that returns pair!

pair<int, char>& operator(int index) const

pair is built in container that has members first and second. So, this is how it would work:

Base my_base;
//...some code between
pair<int, char> store_info;
store_info = my_base[3];
cout<<"This is int on position 3: "<<store_info.first<<endl
     <<"This is char on position 3: "<<store_info.second<<endl;

And one more thing about declaring operators.
Basically, your [] operator won't change object members, so it's best to make it const (that last const).
const keyword is not needed in argument, since it's argument you are passing by value, which means it can never get changed.

After all what's your real problem?! If you want to access these two array members make them public, not private. What for these members were declared as private if you waste two weeks in inventing of method to get full access references to these "private" members via absurd "overloading subscript operator for arrays"?!..

This article has been dead for over six months. Start a new discussion instead.