pure-virtual

Deriving an abstract class from concrete class

Let's say we have a concrete class Apple. (Apple objects can be instantiated.) Now, someone comes and derives an abstract class Peach from Apple. It's abstract because it introduces a new pure virtual function. The user of Peach is now forced to derive from it and define this new function. Is this a common pattern? Is this correct to do?...

How do you declare an interface in C++?

How do I setup a class that represents an interface? Is this just an abstract base class? ...

DECLSPEC_NOVTABLE on pure virtual classes?

This is probably habitual programming redundancy. I have noticed DECLSPEC_NOVTABLE ( __declspec(novtable) ) on a bunch of interfaces defined in headers: struct DECLSPEC_NOVTABLE IStuff : public IObject { virtual method1 () = 0; virtual method2 () = 0; }; The MSDN article on this __declspec extended attribute says that adding ...

Pure Virtual Function Call

I obviously do not 'grok' C++. On this programming assignment, I have hit a dead end. A runtime error occurs at this line of code: else if (grid[i][j]->getType() == WILDEBEEST) { ... with the message "Runtime Error - pure virtual function call." From my understanding, this error occurs if the function reference attempts to call the...

Is it possible to create a vector of pointers?

Just wondering, because of a problem I am running into, is it possible to create a vector of pointers? And if so, how? Specifically concerning using iterators and .begin() with it, ie: How would I turn this vector into a vector of pointers: class c { void virtual func(); }; class sc:public c { void func(){cout<<"using func...

What is the purpose of __cxa_pure_virtual?

Whilst compiling with avr-gcc I have encountered linker errors such as the following: undefined reference to `__cxa_pure_virtual' I've found this document which states: The __cxa_pure_virtual function is an error handler that is invoked when a pure virtual function is called. If you are writing a C++ application that has pure...

Under what circumstances is it advantageous to give an implementation of a pure virtual function?

In C++, it is legal to give an implementation of a pure virtual function: class C { public: virtual int f() = 0; }; int C::f() { return 0; } Why would you ever want to do this? Related question: The C++ faq lite contains an example: class Funct { public: virtual int doit(int x) = 0; virtual ~Funct() = 0; }; inline Funct::...

Why would I want to use a pure virtual function in C++?

I'm learning about C++ in a class right now and I don't quite grok pure virtual functions. I understand that they are later outlined in a derived class, but why would you want to declare it as equal to 0 if you are just going to define it in the derived class? ...

Why do we need a pure virtual destructor in C++?

I understand the need for a virtual destructor. But why do we need a pure virtual destructor? In one of the C++ articles, the author has mentioned that we use pure virtual destructor when we want to make a class abstract. But we can make a class abstract by making any of the member functions as pure virtual. So my questions are Wh...

C++ Collection of instances implementing a pure virtual class

Hello, I am working in cross platform C++, and have some classes defined like so: (heavily simplified for this example) class ExampleBase { public: ExampleBase( int blah ) : blah_test(blah) { } virtual void DoSomething( ) = 0; private: int blah_test; }; class ExampleImplementer : public ExampleBase { public: ExampleIm...

Pure Virtual Method Called

EDIT: SOLVED I'm working on a multi-threaded project right now where I have a base worker class, with varying worker classes that inherit from it. At runtime, the worker classes become threads, which then perform work as needed. Now, I have a Director I've written which is supposed to maintain an array of pointers to all of the workers...

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

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 abst...

Sub-classing templated class without implementing pure virtual method

I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {} virtual ~QueueBC() {} private: virtual IItemBuf* constructItem(const QueueItemT& item) = 0; } I created the following sub-class: class MyQueue : public QueueBC<MyItemT> { public: MyQueue() {} virtual...

Abstract classes in shared library

I have an ordinary abstract class that has a couple of pure virtual methods. The class itself is a part of the shared library. The compilation of the shared library itself is OK. But when the library is linked to another program that has another class deriving from the abstract one in the shared library and defining the pure virtual meth...

Where in the standard is forwarding to a base class required in these situations?

Maybe even better is: Why does the standard require forwarding to a base class in these situations? (yeah yeah yeah - Why? - Because.) class B1 { public: virtual void f()=0; }; class B2 { public: virtual void f(){} }; class D : public B1,public B2{ }; class D2 : public B1,public B2{ public: using B2::f; }; class D3 : publ...

pure-specifier on function-definition

While compiling on GCC I get the error: pure-specifier on function-definition, but not when I compile the same code using VS2005. class Dummy { //error: pure-specifier on function-definition, VS2005 compiles virtual void Process() = 0 {}; }; But when the definition of this pure virtual function is not inline, it works: class ...

keeping private parts outside c++ headers: pure virtual base class vs pimpl

Hi all, I recently switched back from Java and Ruby to C++, and much to my surprise I have to recompile files that use the public interface when I change the method signature of a private method, because also the private parts are in the .h file. I quickly came up with a solution that is, I guess, typical for a Java programmer: interfa...

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

Hi, 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 c...

pure virtual function and abstract class

I have the following classes, Base and Derived and when I compile the compiler complains that it cannot create an instance of DLog because it is abstract. Can someone tell me how I can fix this error? I'm guessing it's because not both pure virtual functions are not implemented in the Derived. class Logger { public: virtual void ...

"import" a definition of a function from a base class to implement abstract interface (multiple inheritance in C++)

Say we have a class inheriting from two base classes (multiple inheritance). Base class A is abstract, declaring a pure virtual function foo, the other base class B declares and implements a function foo of the very same signature. struct A { virtual void foo(int i) = 0; }; struct B { virtual void foo(int i) {} }; struct C : publi...