pure-virtual

Calling class T's implementation of pure virtual from T constructor without qualification?

本小妞迷上赌 提交于 2020-01-04 05:28:20
问题 Considering that a virtual call of a T member function (directly or indirectly) from a constructor of a class T , can at most go down to T 's implementation, does the following code, with unqualified call , have Undefined Behavior or not? Note, to avoid noise: if you believe that member functions are not called virtually when invoked from a constructor, then please don't answer or comment here, but raise that issue in a separate SO question. Thank you. struct Baze { virtual void foo();

C++ - calling derived function from abstract base pointer

不问归期 提交于 2020-01-03 17:26:08
问题 I have been trying to create a TCP Server model based on inheritance, with varying success. These servers are managed by a singleton whose task it is to shut these servers down and other simple maintenance functions: class TCPServer { public: TCPServer(); ~TCPServer(); void Bind(TCPDaemon *daemon) { if(!daemon->IsRunning()) { throw TCPBindException("Daemon is inactive"); } // if the port is not taken, bind this daemon to it if(this->servers.count(daemon->port())==0) { this->servers[daemon-

C++ abstract class without pure virtual functions?

烂漫一生 提交于 2019-12-28 06:30:09
问题 I have a base class class ShapeF { public: ShapeF(); virtual ~ShapeF(); inline void SetPosition(const Vector2& inPosition) { mPosition.Set(inPosition); } protected: Vector2 mPosition; } Obviously with some ommitied code, but you get the point. I use this as a template, and with some fun (ommited) enums, a way to determine what kind of shape i'm using class RotatedRectangleF : public ShapeF { public: RotatedRectangleF(); virtual ~RotatedRectangleF(); protected: float mWidth; float mHeight;

Pure virtual functions may not have an inline definition. Why?

守給你的承諾、 提交于 2019-12-27 17:07:52
问题 Pure virtual functions are those member functions that are virtual and have the pure-specifier ( = 0; ) Clause 10.4 paragraph 2 of C++03 tells us what an abstract class is and, as a side note, the following: [Note: a function declaration cannot provide both a pure-specifier and a definition —end note] [Example: struct C { virtual void f() = 0 { }; // ill-formed }; —end example] For those who are not very familiar with the issue, please note that pure virtual functions can have definitions but

How to inherit and implement a pure virtual method with the abstract class as a parameter?

白昼怎懂夜的黑 提交于 2019-12-24 04:53:08
问题 I have an abstract class Node which contains a pure virtual method stub matches , requiring another instance of a Node (i.e. instance of something that subclasses Node ) as a parameter. class Node; // forward declaration class Node { public: Node() : parentNode(this) {} virtual ~Node() {} Node* parentNode; virtual bool matches(const Node& node) const = 0; }; How can I implement matches in a subclass such that the parameter can be of the subclasses type as opposed to Node ? E.g. I want

Deriving a class from an abstract class (C++)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 02:42:26
问题 I have an abstract class with a pure virtual function f() and i want to create a class inherited from that class, and also override function f(). I seperated the header file and the cpp file. I declared the function f(int) in the header file and the definition is in the cpp file. However, the compiler says the derived class is still abstract. How can i fix it? 回答1: The functions f() and f(int) do not have the same signature, so the second would not provide an implementation for the first. The

Can a class still be pure abstract if it has a non-pure destructor?

旧巷老猫 提交于 2019-12-22 18:05:11
问题 I am working on an exercise which asks me to take a base class Rodent and make it a pure abstract class. My understanding of a pure abstract class is that it acts as an interface and only contains pure virtual functions. Although this is an easy exercise I have a problem with the solution provided by the book: class Rodent { public: virtual ~Rodent() {cout << "Destroy rodent" << endl;} virtual void run() = 0; virtual void squeak() = 0; }; As you can see the author has added a dummy definition

How to fix pure virtual function called runtime error?

流过昼夜 提交于 2019-12-22 10:53:35
问题 I understand why I am getting the error I am getting (pure virtual function called). I am trying to call pure virtual functions from within the destructor of my base class shown below. However, I do not know how to rework my code to prevent this from happening. Here are the base and derived classes (the relevant portions anyway): Base class: TailFileManager::TailFileManager(const std::string &filename, const int fileOpenPeriod_ms) : m_Stop(false) { m_WorkerThread.reset(new boost::thread(boost

C++11 interface pure virtual destructor

心已入冬 提交于 2019-12-21 15:18:14
问题 UPD . There is a mark that it is a duplicate of this question. But in that question OP asks HOW to use default to define pure virtual destructor. This question is about what the difference . In C++ (latest standard if possible) what the real difference between defining pure virtual destructor with empty body implementation and just a empty body (or default)? Variant 1: class I1 { public: virtual ~I1() {} }; Variant 2.1: class I21 { public: virtual ~I21() = 0; }; I21::~I21() {} Variant 2.2:

C++ override pure virtual method with pure virtual method

删除回忆录丶 提交于 2019-12-21 06:59:38
问题 Does it ever make sense to override a pure virtual method with another pure virtual method? Are there any functional differences or perhaps code style reasons to prefer one of the following options over the other? class Interface { public: virtual int method() = 0; }; class Abstract : public Interface { public: int method() override = 0; }; class Implementation : public Abstract { public: int method() override { return 42; } }; Versus: class Interface { public: virtual int method() = 0; };