pure-virtual

C++ 11 Delegated Constructor Pure Virtual Method & Function Calls — Dangers?

帅比萌擦擦* 提交于 2019-11-29 17:16:41
问题 Not a Duplicate of Invoking virtual function and pure-virtual function from a constructor: Former Question relates to C++ 03, not new Constructor Delegation behavior in C++ 11, and the question does not address the mitigation of undefined behavior by using delegation to ensure proper construction before pure virtual implementations are executed. In C++ 11, what are the dangers of invoking Pure Virtual functions in a class' constructor, during construction, but after the class/object has been

“Cannot allocate an object of abstract type” error

为君一笑 提交于 2019-11-29 16:02:32
问题 Error is here: vector<Graduate *> graduates; graduates.push_back(new AliceUniversity(identifier,id,salary,average)); Grandparent class: Graduate::Graduate(char identifier, long id, int salary, double average) : _identifier(identifier), _id(id),_salary(salary), _average(average) { } Parent class: UniversityGraduate::UniversityGraduate(char identifier, long id, int salary, double average) : Graduate(identifier,id,salary,average) { } Actual/child class: AliceUniversity::AliceUniversity(char

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

主宰稳场 提交于 2019-11-29 15:30:12
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 : public A, public B {}; I want to use the implementation of foo from base class B in my derived class C . However, if I do not implement the function foo a second time in my derived class C , I cannot instantiate any object of it (it remains abstract). Virtual inheritance

Pure virtual functions in C++11

╄→гoц情女王★ 提交于 2019-11-29 10:37:58
问题 In C++98, the null pointer was represented by the literal 0 (or in fact any constant expression whose value was zero). In C++11, we prefer nullptr instead. But this doesn't work for pure virtual functions: struct X { virtual void foo() = nullptr; }; Why does this not work? Would it not make total sense? Is this simply an oversight? Will it be fixed? 回答1: Because the syntax says 0 , not expression or some other non-terminal matching nullptr . For all the time only 0 has worked. Even 0L would

What does it mean to set the declaration of a function equal to 0? How can you assign an integer to a function?

徘徊边缘 提交于 2019-11-28 21:59:34
问题 I was browsing through the sources of a (prefer not to name) GUI Toolkit which wrapped up the Windows API when I found the following function definition in the window class: virtual LRESULT CALLBACK wndProc (HWND, UINT, WPARAM, LPARAM) = 0; What is happening here? How can you assign a function to an integer? Or does it assign it to NULL ? Do you need to do this if you want to use function pointers in the wndproc? 回答1: That line of code defines a pure virtual function in C++. It has nothing to

What can cause a pure virtual function call in C++?

一曲冷凌霜 提交于 2019-11-28 21:06:43
I teach a C++ programming class and I've seen enough classes of errors that I have a good feeling for how to diagnose common C++ bugs. However, there's one major type of error for which my intuition isn't particularly good: what programming errors cause calls to pure virtual functions? The most common error I've seen that causes this is calling a virtual function from a base class constructor or destructor. Are there any others I should be aware of when helping debug student code? "The most common error I've seen that causes this is calling a virtual function from a base class constructor or

How to resolve “pure virtual method called”

拈花ヽ惹草 提交于 2019-11-28 20:33:43
I understand why this is happening, but I'm stuck trying to resolve it...here is what my code is doing when the error is generated (thus, leading to a crash) when my program exits... pure virtual method called SomeClass::~SomeClass() { BaseClassObject->SomePureVirtualMethod(this); } void DerivedClass::SomePureVirtualMethod(SomeClass* obj) { //Do stuff to remove obj from a collection } I never have a call to new SomeClass but I have a QList<SomeClass*> which I append SomeClass* objects to. The purpose of this destructor in SomeClass is to tell DerivedClass to remove a specific instance of

Why do gcc and clang allow me to construct an abstract class?

寵の児 提交于 2019-11-28 10:43:40
The following code compiles on a wide range of gcc and clang versions - when compiled and run with gcc 5.3.1, it prints A() then aborts with a pure virtual call error. #include <stdio.h> class A { public: A() { printf("A()\n"); } virtual void b() const = 0; }; int main() { const A& a{}; a.b(); return 0; } I realise binding a reference to a temporary is not ideal (though I think this case is covered by some sort of lifetime extension) - but it also works when trying to call a method that takes a const reference like: Foo({}); For convenience here's an example of it compiling with clang 3.2:

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

那年仲夏 提交于 2019-11-28 08:51:54
问题 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 : public A, public B {}; I want to use the implementation of foo from base class B in my derived class C . However, if I do not implement the function foo a second time in

pure-specifier on function-definition

时光怂恿深爱的人放手 提交于 2019-11-28 07:13:09
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 Dummy { virtual void Process() = 0; }; void Dummy::Process() {} //compiles on both GCC and VS2005 What does the error means? Why cannot I do it inline? Is it legal to evade the compile issue as shown in the second code sample? Ok, I've just learned something. A pure