I don't even know if the idea of argument forwarding is an actual topic, or if the term describes properly what I have in mind. I'll just start with an example.
class sort_of_vector
{
// Some data here. Much data.
size_t size;
double property1(unsigned int index)
{
// Do some computation on the index and data.
}
double property2(unsigned int index)
{
// Do some computation on the index and data.
}
};
Here, sort_of_vector
is, indeed, a sort of vector, or looks like it is some sort of vector. It represents a collection of numbered elements, each of which has two (say double) properties. As you can see, those properties are however accessible only through getter methods, because some complicated computation needs to be carried out before returning the actual value.
Now, what I would like to do is to make sort_of_vector
more similar, in terms of interfaces, to an actual vector. I would like a [] operator
to be callable on that, so I could then call a getter for property1
and property2
on something returned via the [] operator
.
There is a trivial way to do this:
class sort_of_vector
{
// Same as before
sort_of_vector_proxy operator [] (unsigned int index)
{
return sort_of_vector_proxy(this, index);
}
};
class sort_of_vector_proxy
{
sort_of_vector * vector;
unsigned int index;
sort_of_vector_proxy(sort_of_vector * vector, unsigned int index)
{
this->vector = vector;
this->index = index;
}
double property1()
{
return this->vector->property1(this->index);
}
double property2()
{
return this->vector->property1(this->index);
}
};
This obviously works, but it has some issues in terms of performance. In principle, accessing the property1
or property2
of some item would mean make a whole new sort_of_vector_proxy
set its properties, call the actual getter through it and delete it.
So I was wondering if there were more performing solutions that can make my sort_of_vector
more similar to a vector in this kind of fashion (my_sort_of_vector[33].property1()
) but without having any performance downside.