Sometimes coming up with a good title for topic seems harder than the problem
itself in the topic.
I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
template<class T, class dataType>
class someContainer;
template<class T, class dataType>
class type_referencer {
someContainer<T, dataType> *data;
size_t pos;
public:
T *getData() { return data->getData() + pos; }
};
template<class T>
class type_data {
T *data;
public:
T *getData() { return data; }
};
template<class T, class dataType>
class someContainer : public dataType { public:
void someFunction() {
T *data = this->getData();
// do stuff with data...
}
};
| |
Lets assume I would wanna create a new string class where this string class
can be of two types. Actual string and a referencer string containing the position of string where its referencing.
1 2
|
void someFunction() {
T *data = this->getData();
| |
*if class dataType is type_data then
instead of creating another variable and copying its value
from type_data::T *data to T *data, i want it to reference to type_data::T *data instead.
*if class dataType is type_referencer then I want
it to do what well, what it should do if no optimiziations are applied.
It would be pain to loop through (data+pos)[i] but efficient
when pointer*ptr=data+pos would loop through ptr[i] instead.
The question here is, do optimizer do it for me or do I have to implement
something special to my code to make this happen?