virtual-functions

virtual function issue

Hello everyone, I am using native C++ with VSTS 2008. A quick question about virtual function. In my sample below, any differences if I declare Foo as "virtual void Foo()" or "void Foo()" in class Derived? Any impact to any future classes which will derive from class Derived? class Base { public: Base() { } virtual vo...

How to override virtual function in good style? [C++]

Hi, guys I know this question is very basic but I've met in few publications (websites, books) different style of override virtual function. What I mean is: if I have base class: class Base { public: virtual void f() = 0; }; in some publications I saw that to override this some authors would just say: void f(); and some would ...

C++ header file and function declaration ending in "= 0"

hi, I have the following code inside the .h file and I'm not sure what does the assignment statement do and how is it called properly? virtual void yield() = 0; I thought that the function returns a value of 0 by default but since this function returns void I am a little bit confused. Can anyone comment on this and maybe say how can ...

Accessing subclass members from a superclass pointer C++

I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them. The problem is, because thes...

Use-cases of pure virtual functions with body?

I recently came to know that in C++ pure virtual functions can optionally have a body. What are the real-world use cases for such functions? ...

Behavior of virtual function in C++

Hi everyone: I have a question, here are two classes below: class Base{ public: virtual void toString(); // generic implementation } class Derive : public Base{ public: ( virtual ) void toString(); // specific implementation } The question is: If I wanna subclass of class Derive perf...

Pointers to Derived Class Objects Losing vfptr

To begin, I am trying to write a run-of-the-mill, simple Ray Tracer. In my Ray Tracer, I have multiple types of geometries in the world, all derived from a base class called "SceneObject". I've included the header for it here. /** Interface for all objects that will appear in a scene */ class SceneObject { public: mat4 M, M_inv; Col...

QWidget keyPressEvent override

Hi there, I'm trying for half an eternity now overriding QWidgets keyPressEvent function in QT but it just won't work. I've to say i am new to CPP, but I know ObjC and standard C. My problem looks like this: class QSGameBoard : public QWidget { Q_OBJECT public: QSGameBoard(QWidget *p, int w, int h, QGraphicsScene *s); signals: v...

Question on Virtual Methods

IF both methods are declared as virtual, shouldn't both instances of Method1() that are called be the derived class's Method1()? I am seeing BASE then DERIVED called each time. I am doing some review for an interview and I want to make sure I have this straight. xD class BaseClass { public: virtual void Method1() { cout << "Method...

virtual function == function pointer?

A set of function pointers grouped into a data structure are often referred to as a virtual function table (VFT). The above statement makes me feel that virtual function == function pointer,is that so? ...

Virtual functions and templates in C++ - can they be replaced with other (existing in C++) operations?

For example concept of Templates in C++ are for comfort as compiler generates some additional code, for your class or for your function, isn't it? So we could live without template by doing some additional (manual work). What about virtual functions??? Are there any situations where both of them are irreplaceable? ...

Why I have to redeclare a virtual function while overriding [C++]

#include <iostream> using namespace std; class Duck { public: virtual void quack() = 0; }; class BigDuck : public Duck { public: // void quack(); (uncommenting will make it compile) }; void BigDuck::quack(){ cout << "BigDuckDuck::Quack\n"; } int main() { BigDuck b; Duck *d = &b; d->quack(); } ...

syntax for calling virtual functions outside of the class?

Hi there, what is the syntax for defining virtual functions outside the class body? class random{ public: random(int i = 0); virtual ~random(){}; virtual void print() const; protected: int id; }; is it? virtual void random::print() { } ? ...

Abstract class reference

Can i have a class Class Room{ ~Room(); virtual cost() =0; } Class Hotel{ map rooms; /* */ }; will my hotel become abstract ? Can it hold the list of concrete Room objects that are derived from Room ? ...

Visual C++ Compiler Option To Dump Class Hierarchy

Is there any compiler option in MS Visual C++ equivalent to GCC's -fdump-class-hierarchy? i.e. showing the virtual function table layout. ...

Unresolved external symbol on virtual function

Each of these classes are in separate dll modules: class Base { public: virtual void foo(); void bar(); }; class Derived : public Base { public: // override Base::foo() void foo(); }; In Other.cpp: #include "Derived.h" The module that contains Derived links with Base.lib (not shown are the export/import directives). The m...

C++ destructor problem with boost::scoped_ptr

I have a question about the following code: #include <iostream> #include <boost/scoped_ptr.hpp> class Interface { }; class A : public Interface { public: A() { std::cout << "A()" << std::endl; } virtual ~A() { std::cout << "~A()" << std::endl; } }; Interface* get_a() { A* a = new A; return a; } int main(...

Virtual functions should not be used excessively - Why ?

I just read that we should not use virtual function excessively. People felt that less virtual functions tends to have fewer bugs and reduces maintenance. I'm not able to get what kind of bugs and disadvantages can appear due to virtual functions. EDIT : One reason I can think of is virtual function may be slow than normal function due...

How to get every virtual function index just as the compiler does?

Is there some plugin or tool which can read a .h file (or simply modify Intellisense itself) and spit out every function and it's virtual function table index? There's a pattern which I have yet to figure out having to do with polymorphism, and it gets 5x harder when you start to have 5 classes or more deriving from each other. No matter...

Why doesn't g++ pay attention to __attribute__((pure)) for virtual functions?

According to the GCC documentation, __attribute__((pure)) tells the compiler that a function has no side-effects, and so it can be subject to common subexpression elimination. This attribute appears to work for non-virtual functions, but not for virtual functions. For example, consider the following code: extern void f( int ); class ...