gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 16:25:29 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com