pure-virtual

Calling pure virtual function [duplicate]

蓝咒 提交于 2019-12-21 02:46:09
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Calling virtual functions inside constructors Look at this code. In the constructor of Base class, we can call the pure virtual function using 'this' pointer. Now when I want to create a typed pointer to the same class and casting 'this' to the same type. It throws run time exception 'pure virtual function call exception'. Why is this so ? #include <iostream> using namespace std; class Base { private: 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-20 10:21:17
问题 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

What are the uses of pure virtual functions in C++?

柔情痞子 提交于 2019-12-20 09:46:22
问题 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? 回答1: Briefly, it's to make the class abstract, so that it can't be instantiated, but a child class can override the pure virtual methods to form a concrete class. This is a good way to define an interface in C++. 回答2: This forces a

pure virtual function and abstract class

試著忘記壹切 提交于 2019-12-20 04:26:15
问题 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

const qualifier disappears from pure virtual function [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-20 03:17:31
问题 This question already has answers here : Top-level const doesn't influence a function signature (7 answers) Closed 5 years ago . Using gcc version 4.8.2: I'm running into an issue where the const qualifier on my parameters is disappearing when I compile my code. Here is an example: main.cc: #include <iostream> class Base { public: virtual int getSum( const int number ) = 0; }; class Derived : public Base { public: Derived( const int& num ) : _myNumber( num ) {} virtual int getSum( const int

Pure virtual invocation from constructor and destructor

瘦欲@ 提交于 2019-12-19 05:08:31
问题 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? 回答1: 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

Why a pure virtual destructor needs an implementation

做~自己de王妃 提交于 2019-12-18 15:35:43
问题 I know the cases where pure virtual destructors are needed. I also know that If we don't provide an implementation for them it will give me a linker error. What I don't understand is why this should be the case in a code fragment as shown below: int main() { Base * p = new Derived; } Here there is no delete, so no call to destructor and so no need for its implementation(assuming it is supposed to behave like other normal functions which are declared but not defined, linker complains only when

Error: expected type-specifier before 'ClassName'

走远了吗. 提交于 2019-12-17 17:54:20
问题 shared_ptr<Shape> circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0))); shared_ptr<Shape> rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0, Vec3f(1.0f, 1.0f, 0)) ); I'm trying to understand why the above won't compile. For Whatever reason, when I try to create an instance of Rect2f (which DOES inherit from the Shape class specified the shared_ptr template argument, just like Circle ), I get the following errors : error: expected type-specifier before 'Rect2f' error: expected ')' before 'Rect2f'

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

末鹿安然 提交于 2019-12-17 15:54:17
问题 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::~Funct() { } // defined even though it's pure virtual; it's faster this way; trust me I don't understand why the destructor is declared pure virtual and then implemented; and

pure-specifier on function-definition

我的未来我决定 提交于 2019-12-17 10:53:10
问题 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