pure-virtual

C++: call pure virtual function from member function of same class

痞子三分冷 提交于 2019-12-07 06:11:10
问题 Consider the following 2 programs. #include <iostream> using std::cout; class Base { public: virtual void f()=0; void g() { f(); } virtual ~Base() { } }; class Derived : public Base { public: void f() { cout<<"Derived::f() is called\n"; } ~Derived() {} }; class Derived1 : public Base { public: void f() { cout<<"Derived1::f() is called\n"; } ~Derived1() { } }; int main() { Derived1 d; Base& b=d; b.g(); b.f(); } Compiles & runs fine and gives expected outcome.. #include <iostream> using std:

C++ pure virtual methods

时光总嘲笑我的痴心妄想 提交于 2019-12-07 05:59:25
问题 Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0; virtual int f(){ return f(0); } virtual ~Base(){ } }; class Derived : public Base { public: int f(int i) { return (10 + i); } }; int main(void) { Derived obj; printf("%d\n", obj.f(1)); // This works, and returns 11 printf("%d\n", obj.f()); // Adding this line gives me the error listed below } Which gives me the following compilation error: virtualfunc.cpp: In function ‘int main()’: virtualfunc.cpp:25:26

“pure virtual method called” when implementing a boost::thread wrapper interface

拈花ヽ惹草 提交于 2019-12-07 03:54:24
问题 I have a small wrapper which centralize what's relative to threads : class Thread { protected: boost::thread *m_thread; virtual void work() = 0; void do_work() { work(); } public: Thread() : m_thread(NULL) {} virtual ~Thread() { catch_up(); delete m_thread; } inline void catch_up() { if(m_thread != NULL) { m_thread->join(); } } void run() { m_thread = new boost::thread(boost::bind(&Thread::do_work, boost::ref(*this))); } }; When I implement it, say with the following : class A : public Thread

Pure virtual function call

半世苍凉 提交于 2019-12-07 02:54:46
问题 I'm using boost.python to make python-modules written in c++. I have some base class with pure virtual functions which I have exported like this: class Base { virtual int getPosition() = 0; }; boost::python::class_<Base>("Base") .def("GetPosition", boost::python::pure_virtual(&Base::getPosition)); in Python I have code: class Test(Base): def GetPosition(self): return 404 Test obj obj.GetPosition() RuntimeError: Pure virtual function called What's wrong? 回答1: This error happens when a

R6025 pure virtual function call

微笑、不失礼 提交于 2019-12-06 15:48:03
问题 I am getting random R6025 - pure virtual function call errors at random times when using this custom c# RTD client for ThinkOrSwim. How can I a) debug it to find out what's going wrong, and b) fix it? When windows pops up the message box saying there is an error, the code keeps running in the backgroud and no exceptions are thrown. But when I click OK on the message box, windows shuts the application down. Here is a snippet of the code requesting RTD data: var tosClassId = new Guid(Registry

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

坚强是说给别人听的谎言 提交于 2019-12-06 06:45:00
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 for the destructor. Does the adding of this definition not mean that this is an abstract class and not

How to use clone() in C++ with multiple inheritance of abstract classes?

廉价感情. 提交于 2019-12-05 21:51:36
I am working on a C++ program, but I am having problem with multiple inheritance when using cloning. The problem (in a simplified form) is the following. I want to be able to clone all objects derived from the class Base. class Base{ public: virtual Base* clone()const=0; }; I want to define two other classes derived from Base, which are both abstract, i.e. I cannot define the clone function, but I have to declare them in some way (I want to make sure, that if I clone Derived*, I will get back Derived* and not Base*, i.e. I want to avoid casting at the point of application) class Derived1:

How to fix pure virtual function called runtime error?

十年热恋 提交于 2019-12-05 20:02:57
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::bind(&TailFileManager::TailFile, this, filename, fileOpenPeriod_ms))); } TailFileManager::

Should an abstract class' destructor be pure virtual?

廉价感情. 提交于 2019-12-05 12:18:34
问题 I think virtual alone is generally sufficient. Is there another reason to make it pure virtual than to force derived classes to implement their own destructor? I mean if you allocate something in your class' constructor you should impement your own destructor - if your class is derived or not. Doesn't count as answer as I already know: If you want your class abstract and it has no pure virtual functions - leave it to the destructor. Some more uses? 回答1: If you want your class abstract and it

C++ pure virtual methods

醉酒当歌 提交于 2019-12-05 10:08:50
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0; virtual int f(){ return f(0); } virtual ~Base(){ } }; class Derived : public Base { public: int f(int i) { return (10 + i); } }; int main(void) { Derived obj; printf("%d\n", obj.f(1)); // This works, and returns 11 printf("%d\n", obj.f()); // Adding this line gives me the error listed below } Which gives me the following compilation error: virtualfunc.cpp: In function ‘int main()’: virtualfunc.cpp:25:26: error: no matching function for call to ‘Derived::f()’ virtualfunc.cpp:15:9: note: candidate is: