How to overload operator[] for indexing the shared_ptr elements of a custom vector class?

Currently, I'm creating my own vector class, and I like to create instances of any class via shared pointers. So, is there a way to overload operator[] in order to achieve the following code?

class myVector {
    public:
        myVector(/*some params*/);
        ~myVector();

        // Some kind of overloading of operator []

        // Some other code

    private:
        // private container array
        double* vec;

        // Some other code
};

int main ()
{
    std::shared_ptr<myVector> sp = std::shared_ptr<myVector>(new myVector(/*some params*/));

    double val = sp[1];

    return 0;
}

Upvotes: 1

Views: 1141

Answers (3)

StPiere
StPiere

Reputation: 4243

I would not do this in real application. But just for demonstration, one possibility to achieve what you want (at least technically) is to derive from std::shared_ptr and there declare the index operator. For ex:

#include <memory>
#include <vector>
using namespace std;

template <class T>
class myshared_ptr;

template <class T >
class myshared_ptr<vector<T>> : public shared_ptr<vector<T>>
{
public:
    using elem_type = vector<T>;
    using shared_ptr<elem_type>::shared_ptr;
    using shared_ptr<elem_type>::operator*;
    using shared_ptr<elem_type>::get;
    
    typename elem_type::value_type& operator[](size_t idx)
    {
        return (*get())[idx];
    }
};

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    auto p = myshared_ptr<vector<int>>(new vector<int>{1, 2, 3, 4});
    cout << p[2] << endl;
    return 0;
}

Live Demo

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

You can overload the operator for the class the following way

double & operator []( size_t i )
{
    return vec[i];
}

and

const double & operator []( size_t i ) const
{
    return vec[i];
}

and call it like

std::shared_ptr<myVector> sp = std::shared_ptr<myVector>(new myVector(/*some params*/));

//...

std::cout << ( *sp )[i] << '\n';

Upvotes: 1

Adrian Mole
Adrian Mole

Reputation: 51825

You could declare the [] operator for your myVector class as follows (returning a reference to the element so you have read and write access):

double& operator [] (int i)
{
    return vec[i];
}

You can then use this operator on the object pointed to by the shared_ptr by first dereferencing that. Here's a runnable example, where I've added some 'dummy' code to the constructor just to make it do something:

#include <iostream>
#include <memory>

class myVector {
public:
    myVector(/*some params*/) {
        vec = new double[10];
        for (int i = 0; i < 10; ++i) vec[i] = 3 * i;
    }
    ~myVector() {
        delete[] vec;
    }
    double& operator [] (int i) {
        return vec[i];
    }
 private:
    double* vec;
};

int main()
{
    std::shared_ptr<myVector> sp = std::shared_ptr<myVector>(new myVector(/*some params*/));
    double val = (*sp)[6];
    std::cout << val << std::endl;
    (*sp)[4] = 312;
    std::cout << (*sp)[4] << std::endl;
    return 0;
}

Upvotes: 1

Related Questions