pure-virtual

Why a virtual call to a pure virtual function from a constructor is UB and a call to a non-pure virtual function is allowed by the Standard?

余生颓废 提交于 2019-12-02 20:41:32
From 10.4 Abstract Classes parag. 6 in the Standard : "Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined." Assuming that a call to a non-pure virtual function from a constructor (or destructor), is allowed by the Standard, why the difference ? [EDIT] More standards quotes about pure virtual functions: § 10.4/2 A virtual function is specified pure by using a pure-specifier (9.2

pure virtual function and abstract class

徘徊边缘 提交于 2019-12-02 03:48:19
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 log(int debugLevel, char* fmt, ...) = 0; virtual void log(string logText, int debugLevel, string threadName = "") = 0; static Logger* defaultLogger() {return m_defaultLogger;} static void setDefaultLogger(Logger& logger) {m_defaultLogger = &logger;} protected: static

Pure virtual invocation from constructor and destructor

别说谁变了你拦得住时间么 提交于 2019-12-01 02:31:55
The C++ standard says that invoking a pure virtual function from a constructor or destructor is forbidden. What is the reason for this? Why should the standard place a restriction like this? At the point a class destructor is run, all subclass destructors have already been run. It would not be valid to call a virtual method defined by a subclass, for which its destructor has already run. A similar restriction exists around calling virtual methods in constructors. You can't call a virtual method for a subclass whose constructor has not yet run. It's the same reason you can't live in a house

C++: pure virtual assignment operator

江枫思渺然 提交于 2019-11-30 20:28:48
why if we have pure virtual assignment operator in a base class, then we implement that operator on the derived class, it give linker error on the base class? currently I only have the following explanation on http://support.microsoft.com/kb/130486 , it said that the behavior is by design since normal inheritance rules does not apply . it is not clear for me, why is it generate linker error by design? can someone give me more clear explanation about this? edit: added my simplified code of which the error occured: class __declspec(dllexport) BaseClass { public: int memberA; virtual BaseClass&

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

独自空忆成欢 提交于 2019-11-30 11:45:39
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 "fully constructed" via constructor delegation? Apparently, somewhere in the C++ 11 spec such a

“Cannot allocate an object of abstract type” error

て烟熏妆下的殇ゞ 提交于 2019-11-30 07:58:07
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 identifier, long id, int salary, double average) : UniversityGraduate(identifier,id,salary,average) {

Pure virtual functions in C++11

天涯浪子 提交于 2019-11-30 07:53:08
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? 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 be ill-formed because it does not match the syntax. Edit Clang allows = 0x0 , = 0b0 and = 00 (31.12.2013).

C++: pure virtual assignment operator

走远了吗. 提交于 2019-11-30 05:11:27
问题 why if we have pure virtual assignment operator in a base class, then we implement that operator on the derived class, it give linker error on the base class? currently I only have the following explanation on http://support.microsoft.com/kb/130486 , it said that the behavior is by design since normal inheritance rules does not apply . it is not clear for me, why is it generate linker error by design? can someone give me more clear explanation about this? edit: added my simplified code of

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-30 01:13:37
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? Cody Gray That line of code defines a pure virtual function in C++. It has nothing to do with the otherwise tricky Win32 API or GUI code in general. A pure virtual function is a

Protect CRTP pattern from stack overflowing in “pure virtual” calls

末鹿安然 提交于 2019-11-30 00:31:43
问题 Consider the following standard CRTP example: #include <iostream> template<class Derived> struct Base { void f() { static_cast<Derived *>(this)->f(); } void g() { static_cast<Derived *>(this)->g(); } }; struct Foo : public Base<Foo> { void f() { std::cout << 42 << std::endl; } }; int main() { Foo foo; foo.f(); // just OK foo.g(); // this will stack overflow and segfault } If this was regular virtual inheritance I could have mark virtual f and g methods as pure like struct Base { virtual void