beginner c ++:基类中的虚函数

时间:2022-09-08 16:54:52

I'm writing some code where I defined the following base class.

我正在编写一些代码,我定义了以下基类。

class Chorus{
  public:

    //Destructor
    virtual ~Chorus();

    //callback function
    virtual int callback( void *outputBuffer, void *notUsed, unsigned int 
       nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData );

    virtual void initializeDelayBuffer(void);

    virtual void destroyDelayBuffer(void);
};

I want to use this as a base class and not actually do anything with it on its own. So I have two seperate classes which are derived from this class Chorus. I wanted to do this to simply provide some basic constraints as to what any derived Chorus class MUST have to be considered usable in my program.

我想将它用作基类,而不是实际上对它自己做任何事情。所以我有两个单独的类,这些类派生自这个类Chorus。我想这样做只是简单地提供一些基本约束,以确定任何派生的Chorus类必须被认为在我的程序中可用。

When I build my project (Visual Studio 2008), I get unresolved external symbol errors on all the virtual functions from this Chorus class. I'm guessing it's the typical error where I didn't make forward declarations of these functions. But, since they are virtual and I don't want them to actually be defined to do anything until they are defined within the deriving classes, what do I do to resolve this issue?

当我构建我的项目(Visual Studio 2008)时,我在此Chorus类的所有虚函数上得到了未解析的外部符号错误。我猜这是典型的错误,我没有做出这些函数的前向声明。但是,由于它们是虚拟的,我不希望它们实际上被定义为在导出类中定义之前做任何事情,我该怎么做才能解决这个问题?

6 个解决方案

#1


If your intent is for them to be simply place holders for child classes to implement, then make them pure virtual functions by ending with = 0. For example

如果你的意图是让它们只是实现子类的占位符,那么以= 0结尾使它们成为纯虚函数。例如

virtual void destroyDelayBuffer(void) = 0;

This makes the method "abstract" so to speak. The C++ compiler will not look for an actual definition of the method but instead will force all derived classes to implement these methods before an instance can be created.

这使得该方法可以说是“抽象的”。 C ++编译器不会查找方法的实际定义,而是强制所有派生类在创建实例之前实现这些方法。

#2


You need to declare those function as pure virtual.

您需要将这些函数声明为纯虚拟。

virtual void initializeDelayBuffer(void) = 0;

virtual void initializeDelayBuffer(void)= 0;

That will create an interface of sorts, which is what you want.

这将创建一个各种各样的界面,这是你想要的。

#3


This is called a pure virtual function. In C++ you write a "=0" after the function name, but you probably want to read the FAQ on these.

这称为纯虚函数。在C ++中,你在函数名后面写了一个“= 0”,但是你可能想要阅读关于这些的FAQ。

http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.4

#4


You need to define the functions as pure virtual functions. To do this, add a " = 0" after each declaration. For example:

您需要将函数定义为纯虚函数。为此,请在每次声明后添加“= 0”。例如:

virtual void destroyDelayBuffer(void) = 0;

#5


Use a pure virtual function:

使用纯虚函数:

  virtual int callback( void *outputBuffer, void *notUsed, unsigned int 
       nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData ) = 0;

#6


Just to add to the above answers, you cannot instantiate an object of a class containing pure virtual functions, so in the future if you intend to have objects of your base class, do remember to make the function non-pure virtual.

只是为了添加上述答案,您无法实例化包含纯虚函数的类的对象,因此将来如果您打算拥有基类的对象,请记住将该函数设置为非纯虚函数。

#1


If your intent is for them to be simply place holders for child classes to implement, then make them pure virtual functions by ending with = 0. For example

如果你的意图是让它们只是实现子类的占位符,那么以= 0结尾使它们成为纯虚函数。例如

virtual void destroyDelayBuffer(void) = 0;

This makes the method "abstract" so to speak. The C++ compiler will not look for an actual definition of the method but instead will force all derived classes to implement these methods before an instance can be created.

这使得该方法可以说是“抽象的”。 C ++编译器不会查找方法的实际定义,而是强制所有派生类在创建实例之前实现这些方法。

#2


You need to declare those function as pure virtual.

您需要将这些函数声明为纯虚拟。

virtual void initializeDelayBuffer(void) = 0;

virtual void initializeDelayBuffer(void)= 0;

That will create an interface of sorts, which is what you want.

这将创建一个各种各样的界面,这是你想要的。

#3


This is called a pure virtual function. In C++ you write a "=0" after the function name, but you probably want to read the FAQ on these.

这称为纯虚函数。在C ++中,你在函数名后面写了一个“= 0”,但是你可能想要阅读关于这些的FAQ。

http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.4

#4


You need to define the functions as pure virtual functions. To do this, add a " = 0" after each declaration. For example:

您需要将函数定义为纯虚函数。为此,请在每次声明后添加“= 0”。例如:

virtual void destroyDelayBuffer(void) = 0;

#5


Use a pure virtual function:

使用纯虚函数:

  virtual int callback( void *outputBuffer, void *notUsed, unsigned int 
       nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData ) = 0;

#6


Just to add to the above answers, you cannot instantiate an object of a class containing pure virtual functions, so in the future if you intend to have objects of your base class, do remember to make the function non-pure virtual.

只是为了添加上述答案,您无法实例化包含纯虚函数的类的对象,因此将来如果您打算拥有基类的对象,请记住将该函数设置为非纯虚函数。