c++ - OO C++ - 虚拟方法

标签 c++ oop

这里只是一个非常简短的问题。我正在使用虚函数从文本文件中读入。现在,它是虚拟的,因为一方面我希望这些值被标准化,而另一方面我不希望它们被标准化。我试过这样做:

bool readwav(string theFile, 'native');

因此理论上,如果使用“native”,则应调用此方法,但是,如果调用“double”,则调用该方法的不同版本。如果值为空,则相同,它应该只执行 native 选项。

第一个问题,为什么上面的声明不起作用?另外,这是最好的下山路线吗?或者,只使用一种在选项之间切换的类方法会更好吗?

谢谢:)

更新:

我哪里错了?

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

最佳答案

因为'native' 是多字符字符,不是字符串。不过,我会使用该函数的多个版本:

bool readwavNative(string theFile);
bool readwavDouble(string theFile);

或者至少一个 enum 作为第二个参数:

enum ReadType
{
   ReadNative,
   ReadDouble
};

//...
bool readwav(string theFile, ReadType type);

关于c++ - OO C++ - 虚拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12348909/

相关文章:

c++ - 在创建过程中调用对象方法 C++

python - 面向对象设计?

c++ - 在 C++ 类 : Is it "legit"? 中进行指针数学运算

c++ - C++中的静态与成员函数是否存在开销

javascript - 简单的 JavaScript OOP 类

php - 如何在 HTTP 请求和 cli 类对象之间进行对话

c# - 这对这种情况有意义 - 类 vs 接口(interface) vs 抽象?

java - 在这种情况下,我选择组合而不是继承是正确的吗?

c++ - C++程序如何从Microsoft Excel文件中读取数据

c++ - 将 'this' 地址保存到变量中