[ create a new paste ] login | about

Link: http://codepad.org/9YXChfJT    [ raw code | output | fork ]

C++, pasted on Mar 2:
#include <iostream>
#include <vector>
using namespace std;

class Test
{
  private:
    const int asize;
    vector<double> array;

  public:
    Test(const int asize):asize(asize) { array=vector<double>(asize); }
    int getSize( ) const{ return asize; }

    double& operator[](const int pos){
      //return array[pos];
      return const_cast<double&>( static_cast<const Test>(*this)[pos] );
    }
    const double& operator[](const int pos)const { return array[pos]; }
};

// デバグ用
ostream& operator<<( ostream& os, const Test& obj ){
  for( int i=0; i<obj.getSize(); ++i ){
    os << obj[i] << ( ((i+1)!=obj.getSize())?", ":"" );
  }
  return os;
}


int main(int argc, char* argv[])
{
  Test t0(3); t0[0]=0, t0[1]=1, t0[2]=2;
  cout << t0 << endl; // 0,1,2でない


  return 0;
}


Output:
1
0, 0, 0


Create a new paste based on this one


Comments: