简体   繁体   English

在继承中实现虚拟功能

[英]Implementing virtual functions in inheritance

I have an exercise dealing with classes in c++, in which I create a file system like so ( File.h ) 我有一个练习处理c ++中的类,在其中我创建了一个像这样的文件系统( File.h

class File {
   public:
   virtual string getName();
   virtual void print() const=0;
   virtual bool operator==(const File& file) const=0;
}

Then, I implement getName in File.cpp and create TextFile.h 然后,我在File.cpp实现getName并创建TextFile.h

class TextFile : public File {
   public:
   void print() const;
   void operator==(const TextFile& textFile) const;

   private:
   string text_;

Implement in TextFile.cpp TextFile.cpp实现

void TextFile :: print() const {
   cout << text_ << endl;
}

bool TextFile :: operator==(const TextFile& textFile) const {
   return true; //just for compiling
}

when compiling we get: 编译时,我们得到:

$ g++ -Wall -g File.cpp TextFile.cpp -o  RunMe
TextFile.cpp: In function ‘int main()’:
TextFile.cpp:8:11: error: cannot declare variable ‘Ilan’ to be of abstract type ‘TextFile’
  TextFile Ilan("Ilan", NULL, "Blah \n NewLine");
           ^
In file included from TextFile.cpp:1:0:
TextFile.h:8:7: note:   because the following virtual functions are pure within ‘TextFile’:
 class TextFile: public File
       ^
In file included from TextFile.h:4:0,
                 from TextFile.cpp:1:
File.h:57:18: note:     virtual bool File::operator==(const File&) const
     virtual bool operator==(const File& file) const = 0;

I probably don't know how to work well with inheritance and operator functions (seeing the print function works well), but I can't find the problem when looking through my course material. 我可能不知道如何与继承函数和运算符一起很好地工作(看到print函数工作得很好),但是在浏览课程资料时我找不到问题。

OVERLOAD vs. OVERRIDE... 过载与超载...

In File you declare the virtual function File您声明虚函数

bool operator==(const File & file) const

as pure. 一样纯净。 ( = 0 ). = 0 )。 So File is an abstract class, and so are its subclasses where it is not overridden. 因此File是一个抽象类,不覆盖它的子类也是一个抽象类。

In TextFile , you overload it with a function with the same name ( operator== ) TextFile ,使用具有相同名称的函数( operator==重载它。

bool operator==(const TextFile & textFile) const 

but you don't override it, because the parameters are not the same type. 但您不要覆盖它,因为参数不是同一类型。 Consequently TextFile is an abstract class because 因此, TextFile是一个抽象类,因为

bool TextFile::operator==(const File & file) const

is still not defined. 仍未定义。

EDIT: the compiler can detect such problems if you use the C++11 keyword "override". 编辑:如果您使用C ++ 11关键字“ override”,则编译器可以检测到此类问题。 In TextFile.h : TextFile.h

class TextFile :  public File 
{
   ...
   void print()            const override;
   bool operator== (.....) const override; 
   ...
 }

a message will tell if the functions actually don't override when they are supposed to do. 将显示一条消息,告诉您该功能在实际应该执行时是否没有覆盖。

The compiler is telling you that it can't declare Ilan to be of type "Textfile", because a "virtual function is pure", and then gives you the name of the pure virtual function. 编译器告诉您无法声明Ilan为“文本文件”类型,因为“虚拟函数是纯函数”,然后为您提供纯虚拟函数的名称。

In C++ a pure virtual function is one which has been declared virtual, and for which no implementation is defined. 在C ++中,纯虚函数是已被声明为虚函数且未定义实现的虚函数。 If a class has any pure virtual functions, it is a virtual class, and you cannot create an instance of that class. 如果一个类具有任何纯虚函数,则它是一个虚类,并且您不能创建该类的实例。 This is useful for creating abstract interfaces, which is what you have done with your parent class. 这对于创建抽象接口很有用,这是您对父类所做的。

In your case, you have specified that the == operator is a class operator in the TextFile declaration, but you have defined the == operator in the global namespace. 在您的情况下,您已在TextFile声明中指定==运算符为类运算符,但已在全局命名空间中定义了==运算符。 To correct this, you can either declare the == operator in the global namespace, or define it as part of TextFile (as Ed Heal suggested), but you should prefer the former for the reasons discussed here: Operator overloading : member function vs. non-member function? 若要更正此问题,您可以在全局名称空间中声明 ==运算符,或将其定义为TextFile的一部分(如Ed Heal所建议),但出于此处讨论的原因,您应该更喜欢前者: 运算符重载:成员函数vs.非成员函数?

The line 线

bool operator==(const &TextFile textFile) const {

is incorrect - should be 不正确-应该是

  bool TextFile::operator==(const TextFile& textFile) const {

As it needs to be defined. 由于需要定义。

File.h 文件.h

class File {
   public:
   virtual string getName() const=0;
   virtual void print() const=0;
};

TextFile.h TextFile.h

class TextFile : public File {
   public:
   string getName() const;
   void print() const;
   bool operator==(const TextFile& textFile) const;

   private:
   string text_;
};

TextFile.cpp TextFile.cpp

string TextFile ::getName() const{
   return text_;
}
void TextFile :: print() const {
   cout << text_ << endl;
}

bool TextFile :: operator==(const TextFile& textFile) const {
   return text_==textFile.text_; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM