Method chaining from namespace
Sorry if this question causes any confusion, I am looking to implement
this and do not know the right way to approach such a thing.
For one of my projects I want to implement method chaining. I want to
incorporate the following functions:
.toVector()
.toArray()
.toBool()
...
I have thought about placing these inside a namespace, e.g:
namespace Misc {
template<typename T, typename Inverse>
vector<T> toVector(Inverse begin, Inverser end) {
// ...
// ..
}
// ...
// ...
}
This is because there could be multiple classes, these classes MIGHT be
able to use these functions, so therefore, it has to be OO rather than
implementing each function again and again in different classes.
Let's say I have the following class Wav which reads in the data contained
in a wav file:
class Wav {
public:
Wav();
Wav(string theFileName);
void getWaveData();
protected:
vector<double> data;
};
data is explicitly stored as a vector inside of the class.
In my main I want to be able to do the following:
int main()
{
Wav wave("file.wav");
int* data = wav.getWavData().toArray(); // Method chaining to store as
an array
}
I do not know whether or not this would be possible and if so how I would
approach this without implementing all of the Misc functions over and over
again inside each of the classes. Is there a way to communicate between
the namespace and the class without having to include all of the functions
over and over again?
I hope someone has a suggestion and any questions I will try to answer.
No comments:
Post a Comment