virtual-functions Questions

1

Solved

Most popular example to illustrate why virtual dispatch happens at runtime is when it cannot be determined at compile time which Derived class is going to be created. For example: Base* b = (rand(...
Boru asked 12/6, 2018 at 11:5

1

Solved

This question follows up on https://mcmap.net/q/45145/-why-do-we-need-virtual-functions-in-c One of the classic examples of virtual functions is class Shape { public: virtual string draw() = 0; ...
Cyrillic asked 30/5, 2018 at 0:56

3

Solved

I have a class template where some methods are defined as virtual to give the ability for the user of my class to give an implementation for them in his derived class. Note that in my template clas...
Cusack asked 26/1, 2012 at 12:26

1

Solved

I have a class A that was inherited from class B. So the interface of class A contains some pure virtual functions of class B and some functions of class A. Now I need to make unit tests for class ...
Unipolar asked 16/2, 2018 at 12:55

4

Solved

In C++ we can do this: struct Base { virtual Base* Clone() const { ... } virtual ~Base(){} }; struct Derived : Base { virtual Derived* Clone() const {...} //overrides Base::Clone }; However,...
Longhorn asked 3/8, 2011 at 10:4

3

Solved

Let's assume this scenario in Visual C++ 2010: #include <iostream> using namespace std; struct Base { void Display() { cout << "Base: Non-virtual display." << endl; ...

5

I'm interested to know if there is any viable way to contiguously store an array of polymorphic objects, such that virtual methods on a common base can be legally called (and would dispatch to the ...
Outrank asked 9/10, 2017 at 4:48

2

Solved

I'm working my arse off trying to implement an alternative for vtables using enums and a ton of macro magic that's really starting to mess with my brain. I'm starting to think i'm not walking the r...
Purehearted asked 5/10, 2017 at 7:4

4

Solved

I'm writing in C++ and I want to pass an unknown type (known only in run time) to a pure virtual function: virtual void DoSomething(??? data); where DoSomething is an implementation of a pure vi...
Anallise asked 7/9, 2017 at 6:49

4

Solved

Here is some code from MSDN: // compile with: /target:library public class D { public virtual void DoWork(int i) { // Original implementation. } } public abstract class E : D { public...
Togo asked 6/2, 2013 at 12:9

4

Solved

Consider the following standard CRTP example: #include <iostream> template<class Derived> struct Base { void f() { static_cast<Derived *>(this)->f(); } void g() { static_ca...
Ardelia asked 18/7, 2017 at 9:50

3

Solved

I am reading an awesome awesome C++11 tutorial and the author provides this example while explaining the final keyword: struct B { virtual void f() const final; // do not override virtual void g...
Commandant asked 24/5, 2017 at 8:41

2

Solved

I tried to make a traits to find if a method is virtual: (https://ideone.com/9pfaCZ) // Several structs which should fail depending if T::f is virtual or not. template <typename T> struct Dv...
Vanhook asked 7/4, 2014 at 11:28

2

Solved

I'm getting a runtime error ("memory can't be written") that, after inspection through the debugger, leads to the warning in the tittle. The headers are the following: componente.h: #ifndef COM...
Resinous asked 1/1, 2012 at 20:45

3

Solved

I'm trying to understand how virtual functions work, and I am stuck at a certain part. I have written this small programm: class First { public: virtual void f(int a) { cout << "First!"...
Nagual asked 26/4, 2017 at 20:44

1

Solved

Why do you need (in order to make it compile) the intermediate CloneImplementation and std::static_pointer_cast (see Section 3 below) to use the Clone pattern for std::shared_ptr instead of somethi...
Mushy asked 24/4, 2017 at 10:48

1

Solved

I was reading the C++ docs the other day and noticed that, although literal types must not have virtual members, this does not prevent them from implementing virtual members. Or at least that's wha...
Loving asked 9/3, 2017 at 3:25

4

Solved

I have some C++ code (written by someone else) which appears to be calling the wrong function. Here's the situation: UTF8InputStreamFromBuffer* cstream = foo(); wstring fn = L"foo"; DocumentReader...
Draught asked 25/1, 2011 at 21:20

1

Solved

It is considered good practice to #[derive(Debug)] for most structs you create to aid in debugging. However, this is not possible if your struct contains a type without Debug, such as traits. But i...
Glassman asked 19/2, 2017 at 23:55

3

Solved

consider the following code : struct A { virtual void foo() {} virtual ~A() = 0; }; struct B :public A { virtual void foo() {}; }; A::~A() {}; int main() { A * a = new B(); a->foo(); } ...
Fluorocarbon asked 1/2, 2017 at 10:34

3

I know one cannot use templates for virtual methods in C++ (or vice versa), as for example discussed here and here. Unfortunately I am not sure how to deal with that limitation in my case. We have...
Euroclydon asked 8/1, 2017 at 21:37

7

Solved

Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so? For example: class base { public: virtual in...
Demetricedemetris asked 27/1, 2009 at 18:24

1

Solved

A game engine has this class : class MouseListener{ public : MouseListener(); virtual void OnMouseDown(int mx,int my); virtual void OnMouseUp(int mx,int my); . . . }; Each object that wants to l...
Herminiahermione asked 18/12, 2016 at 13:19

6

Solved

Is it possible to write virtual methods in Java, as one would do in C++? Or, is there a proper Java approach which you can implement that produces similar behavior? Could I please have some exampl...
Corkboard asked 28/12, 2010 at 16:17

7

Solved

In Java: class Base { public Base() { System.out.println("Base::Base()"); virt(); } void virt() { System.out.println("Base::virt()"); } } class Derived extends Base { public Derived() { System...
Mihrab asked 18/11, 2012 at 13:7

© 2022 - 2024 — McMap. All rights reserved.