Just a really quick question here. I'm using virtual functions to read in from a text file. Now, it's virtual because in one aspect I want the values to be normalised, and, in the other respect I don't want them to be normalised. I have tried to do this:
bool readwav(string theFile, 'native');
So in theory, if the 'native' is used, this method should be called, however, if 'double' is called then a different version of the method is called. Same for if the value is empty, it should just perform the native option.
First question, why doesn't the declaration above work? Also, is this the best route to go down? Or, would it be better to have just one class method that switches between the options.
Thanks :)
Update:
Where am I going wrong?
bool Wav::readwav(string theFile, ReadType type = NATIVE)
{
// Attempt to open the .wav file
ifstream file (theFile.c_str());
if(!this->readHeader(file))
{
cerr << "Cannot read header file";
return 0;
}
for(unsigned i=0; (i < this->dataSize); i++)
{
float c = (unsigned)(unsigned char)data[i];
this->rawData.push_back(c);
}
return true;
}
bool Wav::readwav(string theFile, ReadType type = DOUBLE)
{
// Attempt to open the .wav file
ifstream file (theFile.c_str());
cout << "This is the double information";
return true;
}
Because 'native'
is a multi-char character, not a string. I'd go with multiple versions of the function though:
bool readwavNative(string theFile);
bool readwavDouble(string theFile);
or at least an enum
as the second parameter:
enum ReadType
{
ReadNative,
ReadDouble
};
//...
bool readwav(string theFile, ReadType type);
Sounds like what you want is an enumeration with a default parameter.
enum FileType
{
NATIVE=0,
DOUBLE
};
bool readwav(string theFile, FileType type = NATIVE);
Default parameters are present in the function declaration , do not put them in the definition .
bool readwav(string theFile, FileType type)
{
switch(type)
{
case NATIVE: { ... } break;
case DOUBLE: { ... } break;
default: { ... } break;
}
}
This way, calling readwav
without a parameter will use the NATIVE
type by default.
readwav("myfile.wav"); // Uses NATIVE type
readwav("myfile.wav", NATIVE); // Also uses NATIVE
readwav("myfile.wav", DOUBLE); // Uses DOUBLE type
The question has oop in it so I would assume an oop answer is wanted. I think a strategy patter would suit your purpose.
class WavReader
{
public:
WavReader(const std::string fileName)
{
//open file and prepare to read
}
virtual ~WavReader()
{
//close file
}
virtual bool read()=0;
};
class NativeWavReader: public WavReader
{
public:
NativeWavReader(const std::string fileName): WavReader(fileName){}
virtual bool read()
{
//native reading method
std::cout<<"reading\n";
return true;
}
};
NativeWavReader
implements the read
method from the strategy WavReader
, if you want another method you create a class OtherWavReader
reading the file differently.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.