what about overloading the subscript operator to… say sort an array of ints
without templates...

example:

class Whatever
{
private:
int * m_value;
public:
int &operator[](std::size_t index);
};

It's generally a good idea to establish a context before asking a question. Your question looks more like it should be a reply to an existing thread than the starting post of a new thread...

mmmmk... well this is what it would look like if i just wanted a value of m_data returned...

int &Whatever::operator [](std::size_t index)
{
	return m_data[index];
}

but i dont... i want it to cycle through the array and organize it from smallest to largest.
i thought the question was obvious... sorries

>i want it to cycle through the array and organize it from smallest to largest.
Yea, that's a really bad idea. First, it's likely to be inefficient for an operation that people expect to be constant. Second, unless you want to limit your operator to read-only operations, you can expect havoc to ensue when you sort and then the client code overwrites an index they thought was empty but isn't any longer. For example:

#include <algorithm>
#include <iostream>

class Whatever {
private:
  int m_value[10];
public:
  Whatever() { std::fill ( m_value, m_value + 10, 0 );  }
  int& operator[] ( std::size_t index )
  {
    std::sort ( m_value, m_value + 10 );
    return m_value[index];
  }
};

int main()
{
  Whatever blah;

  blah[5] = 123;

  for ( int i = 0; i < 10; i++ )
    std::cout<< blah[i] <<' ';
  std::cout<<'\n';

  blah[2] = 45;

  for ( int i = 0; i < 10; i++ )
    std::cout<< blah[i] <<' ';
  std::cout<<'\n';

  blah[8] = 9; // Oh noes! We just killed 45

  for ( int i = 0; i < 10; i++ )
    std::cout<< blah[i] <<' ';
  std::cout<<'\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.