#class and operator=[]

20 messages · Page 1 of 1 (latest)

teal geyser
#

hi, I have a class and inheritance problem I have this parent function:


template <typename T>
class Indicateur
{
protected:
    T indicateur;
    T data;
    size_t length;

public:
    Indicateur(const T& _data, size_t _length);

    const typename T::value_type operator[](size_t index) const;

    void update_data(const T& _data);

    int size() const;

    virtual void calcul();
};

the function in parent class


template <typename T>
const typename T::value_type Indicateur<T>::operator[](size_t index) const
{
    if (index < indicateur.size())
    {
        return indicateur[index];
    }
    else
    {
        throw std::out_of_range("Index out of range");
    }
}

this class works very well except that I replace the data with a struct which contains vectors so I would like to modify the function with operator=[] to be able to do Myclass.vectorofmystruc[i] but I don’t know how can I do this. And I don't know how to prevent the use of the initial [] function that I inherit

crisp houndBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

broken brook
#

Can't understand.

teal geyser
#

actually this code is good if my final data is a std::vector so I can write my_class[i] but for my new indicator the result of calculation is an struct :

template <typename T>
struct my_struct
{
  std::vector<T> vec_1;
  std::vector<T> vec_2;
}

so now my data is a struct, if I write my_class[i] I think I have an error but I know that it doesn't do what I want. So I would like to find solution to write my_class.vec_1[i] I didn't succeed and I ended up creating simple functions which has the name of a vector of my struct, takes an int as a parameter and returns the value.

template <typename T>
const typename T::value_type my_class<T>::vec_1(size_t index)
{
    if (index < this->indicateur.vec_1.size())
    {
        return this->indicateur.vec_1[index];
    }
    else
    {
        throw std::out_of_range("Index out of range");
    }
}

So finally a find this solution but if it is possible I would like to use [].

#

@broken brook

broken brook
teal geyser
#

no my_class.vec_1(i)

teal geyser
broken brook
#
struct vec_1_wrapper : std::vector<T>
{
    auto& operator()(size_t index) { return operator[](index); } 
};

struct my_struct
{
    vec_1_wrapper vec_1;
};

my_struct s;
s.vec_1(i);
#

But unsure why do you want to do this? Others only know about [] for indexing.

teal geyser
#

yes it for clean and not with parentheses, but I don't quite understand your example

broken brook
#

It means you can still do vec_1.some_function_on_std_vector

#

You were trying to make both a function and field named vec_1 before I think, which would not work.

teal geyser
#

if I would like the [i] write operator[] ?

broken brook
teal geyser
#

okay, thank you

#

!solved