Questions tagged [virtual-inheritance]

Virtual Inheritance is used to solve the Dreaded Diamond Problem associated with multiple inheritance in C++.

Virtual Inheritance is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes a common direct base for the derived class and any other class that derives from it. In other words, if class A is virtually derived from class V, and class B is derived (directly or indirectly) from A, then V becomes a direct base class of class B and any other class derived from A.

Virtual Inheritance is used to solve the problem of Dreaded Diamond Problem in C++.

When Do we use Virtual Inheritance?

Generally, virtual base classes are most suitable when the classes that derive from the virtual base, and especially the virtual base itself, are pure abstract classes. This means the classes above the "join class" have very little if any data.

420 questions
-1
votes
6 answers

Virtual Functions C++ Hacker rank challenge

I'm new to c++ and I was trying to develop my skills through solving challenges on Hacker Rank. This is the challenge I was working on https://www.hackerrank.com/challenges/virtual-functions My solution for the challenge is class Person…
Iman
  • 9
  • 5
-1
votes
3 answers

How to call a method from another inherited class in c#

I have a Class and Method like below public class Wakeup : World { public void MethodA(string) { Log.writeline("Wakeup World"); } } And below one is another class and method in which I am trying to call…
JAbdul
  • 59
  • 1
  • 6
-1
votes
1 answer

error C2512: no appropriate default constructor available

I have a situation like below code, but I am getting error C2512: no appropriate default constructor available error class A { }; class AProxy :public A { A* ptr; public: AProxy(A* var):ptr(var){} }; class B : public A { }; class BProxy…
aga
  • 359
  • 2
  • 4
  • 15
-1
votes
3 answers

multiple virtual inheritance: why isn't the class method ambiguous?

I came across the following c++ code in an online test. #include class A { public: A(int n = 2) : m_n(n) {} public: int get_n() const { return m_n; } void set_n(int n) { m_n = n; } private: int m_n; }; class…
4rcher
  • 49
  • 4
-1
votes
2 answers

Is it good practice to implement virtual methods using multiple inheritance?

I have: class A { virtual void foo() = 0; virtual void bar() = 0; }; class Fooing { virtual void foo() = 0; }; class Barring { virtual void bar() = 0; }; class TallFooing : public Fooing { void foo(){ std::cout << "tall foo"…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
-2
votes
3 answers

why the following code prints 100?

I have the following structs struct A { int i; A() { i = 0; } A(int _i) : i(_i) {} virtual void f() { cout << i; } }; struct B1 : virtual A { B1() : A(1) { f(); } void f() { cout << i+10; } }; struct B2 : virtual A { …
janneob
  • 959
  • 2
  • 11
  • 25
-2
votes
1 answer

Assembly of constructor for virtual inheritance

Here's a simple inheritance usinig a virtual base class (code available on Compiler Explorer). class B { public: int i = 1; }; class D : virtual public B { public: int j = 2; }; void Assign(B *b) { b->i = 2; } int main() { B *b =…
chaosink
  • 1,329
  • 13
  • 27
-2
votes
2 answers

Class Hierarchy with down cast function pointers

TL;DR How can I get the Base::Choose to accept Derived::CustomUserFunction as an argument? The error I'm getting, and I do understand why I get it, but I don't know how to resolve it is: Argument of type "Derived::* (...)" is incompatible with…
-2
votes
2 answers

About virtual function and inheritance

About the following code: class A { A * next; static A* tmp; public: A() : next(tmp) { if (tmp) tmp->next = this; tmp = this; } virtual void print() { if (next) next->print(); …
shinzou
  • 5,850
  • 10
  • 60
  • 124
-2
votes
2 answers

Please explain why "Hello world" is printed 3 times?

Why is "hello world" printted three times? I don't understand clearly about inheritance virtual in struct with C++. #include using namespace std; struct BS{ BS() { cout << "hello world" << endl; } unsigned int…
user2975963
  • 129
  • 6
-2
votes
2 answers

Obscure usages of C++ virtual inheritance

The virtual inheritance in C++ is an useful way to prevent the diamond issue. However, I can't seem to make it work properly in each and every case. This is going to be very hard to explain but I hope I'll manage. Let's present the problem: A…
Michael
  • 1,357
  • 3
  • 15
  • 24
-3
votes
1 answer

Public method in public inheritance becomes private in C++

I have a base class with a public method, but when I try to call it from a derived class which inherits publicly from the base class it becomes private. How is it possible? Shouldn't public inheritance means that public methods are kept…
-4
votes
2 answers

Is there a way to reinterpret_cast to a virtual derived* and calling overriden from parent?

#include template struct printer { virtual const T* get(size_t& sz) const = 0; void print() { size_t sz; const T* _t = get(sz); //tries to access 'this' for (size_t t = 0; t < sz; t++) …
PinkTurtle
  • 6,942
  • 3
  • 25
  • 44
-4
votes
2 answers

Complex multiple inheritance situation

class diagram class A {public: virtual int func();}; class B: virtual public A {}; class C: virtual public A {}; class D: virtual public C {public: virtual int func();}; class E: public B, public D {}; // e is an object of E e->func(); // this will…
-4
votes
2 answers

Why won't my point in Species class point to its heterogeneous class

My code consists of a class animal and 2 sub classes inheriting animal charactistics - amphibian, and fish. The code compiles but the oorder of the deconstructors kills them from bottom to top but i want them to be killed from top to bottom as the…
1 2 3
27
28