OO C++ - Virtual Methods

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;
 }

Answers


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

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);

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.


Need Your Help

How Can I Echo A Managed Attribute With An Object In Magento?

magento magento-1.4 magento-1.5

I am Again here with my learning problem in magento. I created a field in for my products Catalog/Attributes/Manage Attributes. Now I want to display this Field with the product Information. When I...

PRISM: Using MVVM, how to resolve or inject in a constructor objects?

c# mvvm prism unity ioc-container

I'm using MVVM and PRISM. In the project, I've got a common interface called IFoo and others modules should implement this interface and registered it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.