Questions tagged [virtual-inheritance]

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
76 views

How to call the virtual method before calling overriden method outside the class?

The obvious solution is: class GUI{ public: virtual void render() { //Common render code } } class MainGUI : public GUI { private: void render() override { GUI::render(); ...
-1 votes
1 answer
41 views

C++ Multiple virtual inheritance, classes share the same subobject

I want to inherit 'x' from 'class B' and 'y' from 'class C' but both classes share the same 'A' subobject. Is there any solution? #include <iostream> class A { protected: int x; ...
1 vote
2 answers
70 views

Cannot assign derived raw pointer to base unique_ptr

I have some code that looks something this: class Info { public: virtual bool IsHere() = 0; virtual std::wstring GetStr() = 0; }; class WindowsInfo : public Info { public: virtual std:...
  • 2,594
5 votes
0 answers
43 views

How do I remove this ambiguity in hybrid inheritance? [duplicate]

I have a case in project where I need to inherit an abstract class virtually into two different classes which are again inherited into final class but the compiler keeps showing me this error error ...
1 vote
2 answers
118 views

Why static upcast with virtual inheritance is always correct for GCC?

After learnt from : Why can't static_cast be used to down-cast when virtual inheritance is involved? I'm expecting following code give me the result that shows the static_cast is wrong and ...
  • 370
0 votes
0 answers
26 views

How does A*(pointing to a C) vs a C influence the ambiguity? [duplicate]

Why for the pointer its ambiguous but for the object it is not? If i have a C object it compiles, if i have a A pointer pointing to a C object it doesn't. I should use virtual but i want to understand ...
user avatar
0 votes
2 answers
71 views

object constructed as one base class, method calculated as other base class (diamond inheritance and code redundancy)

I have a diamond inheritance scheme in C++ solved through virtual inheritance. The base class is general has two attributes and some method using them. class general { double attr1, attr2; public: ...
  • 129
1 vote
0 answers
30 views

How to correctly resolve parent member in class with virtual inheritance and overloading?

Let's say I have a base class with 2 overloads for the same function; one in protected scope that is purely abstract, and one in public scope that calls the private one, like in the Parent class below:...
0 votes
1 answer
45 views

Why it says what types are incovariant in this code?

TArrayInt is a child of TArray, why I can't return it in polymorph functions? template <class T> class TArray { public: virtual T& operator[](int index) = 0; virtual void push_back(T ...
0 votes
0 answers
31 views

I am not quite convinced how inheriting virtually would solve the Diamond problem in c++ [duplicate]

I know that inheriting virtually would solve the diamond problem but am still not convinced. If the two-child classes are inheriting virtually and you override a method in two child classes, and you ...
  • 27
2 votes
0 answers
79 views

Virtual inheritance constructor arguments

I know that virtual inheritance enters into the realm of "perhaps you should be doing something different," but I sometimes it is unavoidable. I am a little confused about the preferred way ...
2 votes
2 answers
5k views

"Marked as override but does not override" Problem in OOP Code

I am trying to practice OOP in C++ but I am running into an issue regarding overriding of functions. In my Shape2D and Shape3D classes, I have virtual functions which I redefine in the Square and ...
0 votes
0 answers
163 views

Virtual Base Class and Diamond Problem in C++ [duplicate]

Consider the following code:- #include<iostream> using namespace std; class A { public: int a; A() { a = 10; cout<<"Address of a in A "<&...
0 votes
1 answer
76 views

Double Dreadful Diamond Inheritance issue (alternative solutions allowed)

I ended up in a situation lined out below. I have one library that is pure CPP without external libraries, and another project that is an SDK to interface with an external library. "I" in ...
0 votes
2 answers
106 views

Virtual Inheritance: Interfaces and constructors

I am using C++11. I am trying to declare 2 interfaces: B and C, which each declare some functions to be implemented by the child classes. Both interfaces rely on variables and functions which are ...
2 votes
2 answers
78 views

c++ virtual inheritance doesn't work, how do I use the multiple parents' members?

Example of using virtual inheritance class AA { public: AA() { cout << "AA()" << endl; }; AA(const string& name, int _a):n(name),a(_a) {}; AA(const AA& o) :n(...
  • 2,858
1 vote
0 answers
68 views

Virtual Inheritance Base Constructor Mechanics (Diamond Problem) Clarification

The Diamond Problem and Virtual Inheritance are topics that have been discussed endlessly, though while researching them I found myself still uncertain of a few specifics when it comes to calling Base ...
3 votes
0 answers
67 views

Why is "subclass copy constructor calling virtual inherited parent trivial constructor" not recommended

I have a class that virtually inherits from a base class that only has a trivial constructor, but the copy constructor is explicitly deleted. Now I need to provide a copy constructor for this subclass ...
2 votes
1 answer
183 views

C++ Member and vtable order in diamond (multiple) virtual inheritance

I wanted to know the ordering of member variables and vtable pointers in C++ on a diamond virtual inheritance. Consider the below inheritance: class Base { int b; }; class Derived: public virtual ...
3 votes
2 answers
93 views

C++ Multipath Inheritance : Why the access using Base class scope is non-ambiguous?

I am studying C++ and while studying virtual inheritance, I came across following doubt: class A { public: int x; A() { x = 677; } A(int a) { cout << "A con , x= " ...
3 votes
1 answer
182 views

Forced to call the base constructor when using virtual inheritance although it will never be called?

I have a class Base which has a parameterized constructor and two classes Middle1 and Middle2 which virtually inherit from Base (in order to solve the diamond problem). In addition, class Foo inherits ...
  • 11.9k
2 votes
0 answers
154 views

Member function of (public) derived class is inaccessible from main

I have four classes (classic diamond problem in C++). Let's call the grandparent class A, the parent classes B and C and the child class D. Both B and C have a public member function called attack. I ...
  • 221
2 votes
2 answers
99 views

Base to Derived To another Base cast in constructor segfault — invalid code or compiler bug?

This code gives me a segfault sometimes or invalid result on GCC/Clang. However, it works fine on MSVC & ICC. I'm not sure if my code is invalid from the eyes of the standard or if it is a ...
2 votes
1 answer
179 views

Automatic selection between static_cast and dynamic_cast for best performance

I have to use an object factory that creates new objects of several types each of which is derived from the polymorphic base class. The type of each object is known beforehand, but the factory returns ...
  • 15.2k
1 vote
1 answer
100 views

How to handle multiple inheritance when both inherited classes need a distinct member?

I have the following classes: class ServoPart { protected: virtual void doJob(byte* job) = 0; private: bool moving; Servo servo; }; // the following classes only have a constructor so I ...
1 vote
0 answers
43 views

Expressing virtual ancestors in multilevel inheritance

With virtual inheritance it is the most derived object's obligation to initialize not only direct base clases, but all virtual ancestor classes as well. As an example, consider the following class ...
  • 3,967
2 votes
2 answers
160 views

How to create constructor that calls to grand parent constructor only?

I have 3 classes in a hierarchy (call it A, B, and C) where B extends A and C extends B. Class A has a constructor that takes a single argument. The definition of C requires that A's constructor to be ...
  • 2,201
-1 votes
1 answer
44 views

C++ primer 5th ed. Virtual inheritance and ctor-init

On C++ primer 5th Ed. Chapter 18. Multiple and virtual inheritance, I have this question: Exercise 18.30: Define a default constructor, a copy constructor, and a constructor that has an int parameter ...
  • 2,492
0 votes
1 answer
77 views

why size of Class increases in some inconsistent(patterned) way when Virtual Inheritance is used?

This is what i understood : During virtual inheritance :virtual base{} derived class along with inherited data members have to keep vpointer(to keep track of members of base to keep single instance of ...
  • 31
1 vote
3 answers
217 views

Multiple inheritance with two interfaces, one which derives from first, and a derived class implementing the first interface

Sorry for the poor description. Here's the issue: class PureVirtualBase { public: virtual ~PureVirtualBase() {} virtual int IntFn() = 0; }; class PureVirtualDerivedBase : public ...
1 vote
1 answer
240 views

Alternative to virtual inheritance in re-using base template implementation

In a project I'm working on we're using a pattern where we have pure abstract classes defining an interface, and template implementations that may be instantiated with different template parameters, ...
  • 1,149
-4 votes
2 answers
98 views

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

#include <iostream> template<typename T> struct printer { virtual const T* get(size_t& sz) const = 0; void print() { size_t sz; const T* _t = get(sz); //...
  • 6,932
0 votes
1 answer
29 views

Use of virtual inheritance outside diamond inheritance problem to allow leapfrogging

Consider the following program: #include <iostream> #include <string> class B { public: int n; B() : n(0) {} B(int m) : n(m) {} }; class D1 : virtual public B { public: ...
  • 187
1 vote
1 answer
92 views

C++ virtual inheritance and access to a public virtual method whose implementation is in a class whose inheritance is protected

I am using a virtual inheritance to create a class EditablePrimeNumberSet (a set of prime numbers) that inherits the class EditableNumberSet (a set of integers that can be incremented) while only ...
0 votes
1 answer
149 views

wrong constructor called for virtual base class of virtual base class

I'm having a problem with the constructors of a very complex class structure that is spread over several files. Each class has a public default contructor without arguments and a protected contructor ...
2 votes
3 answers
226 views

Understanding the size of virtual derived class

#include <iostream> using namespace std; class A { int a; }; class B1 : virtual public A { int b1; }; class B2 : virtual public A { int b2; }; class C : public B1, public B2 { int c; }; ...
1 vote
1 answer
76 views

C++ - Why isn't this structure of diamond inheritance cause an ambiguity?

I have scanned through dozens of multiple inheritance question but wasn't able to find an answer for this question. I understand why this won't compile: struct B{ virtual void f(){ printf(&...
  • 404
6 votes
2 answers
192 views

Strange default empty constructor on a virtual inheritance behaviour on GCC

I have the following situation of a Derived class with virtual inheritance to a Base class in my code: class Base { int x; public: Base(int x): x{x} {} virtual void f() = 0; }; class ...
8 votes
3 answers
563 views

Why do the constructor of the derived classes want to initialize the virtual base class in C++?

My understanding, for instance reading this, is that the constructor of a derived class does not call its virtual base class' constructor. Here is a simple example I made: class A { protected: ...
  • 2,491
4 votes
1 answer
72 views

Is it necessary to have default ctor for the most-base class in virtual inheritance?

Preliminaries I am writing a high level library for VMEbus modules. I have two levels of abstraction to represent modules: |---------------------| | VBoard | |---------------------| | + ...
  • 621
1 vote
0 answers
53 views

CUDA Illegal memory access when using virtual class [duplicate]

Base / \ / \ / \ Der1 Der2 \ / \ / ...
  • 1,108
0 votes
1 answer
41 views

Is using structs with virtual inheritance a bad idea when reading from EEPROM? (Arduino)

I've run into a strange problem where if I load my data structure from EEPROM it casts it incorrectly. But, if I have both my function calls to the function responsible for saving the data structure ...
1 vote
1 answer
364 views

Why virtual inheritance cause pointer offset?

I know that multiple inheritance and virtual functions cause a pointer offset when casting the derived class pointer to base class pointer due to memory layout. However I cannot figure out why virtual ...
0 votes
1 answer
424 views

virtual function not accessible in derived class

The following code would not compile. GCC complains no matching function for call to ‘derived::print(std::__cxx11::string&). class base { public: virtual void print(float x){} virtual void ...
  • 249
1 vote
0 answers
45 views

C++: implementing public interface function, as a private [duplicate]

I'd like to understand why I'm able to declare a pure virtual method in an Interface class, but when implementing it, I could make it private and the compiler doesn't scream. class I_A { public: ...
  • 187
3 votes
3 answers
136 views

sequence of the constructor of two parent classes

I have one question on the two version of code. The only different is to switch the virtual keyword between the parents class. Is there any reason why this happened? Version one: #include<iostream&...
  • 31
0 votes
1 answer
43 views

Privatising a public method in a subclass of a base class

I'm working on a class hierarchy structure for graphs in which I have this method called disjoint_union. Because the base class contains attributes (like the adjacency list) that need to be updated ...
0 votes
1 answer
68 views

Unable to use virtual function in C++ child Class

I am making a virtual function program where there are three classes Person, Professor and Student. Person is inherited by both Professor an Student publicly. Here I want to input some parameters in ...
  • 21
1 vote
1 answer
60 views

Is this example working with virtual inheritance in C++?

Could I do this? class A { public: virtual void aFoo() = 0; }; class B : virtual public A { public: virtual void aFoo() { ... } }; class D : public A {}; class C : public B, virtual public D {}...
2 votes
1 answer
181 views

Why does using-declared inheriting constructor NOT initialize the virtual base class using a default constructor?

I stumbled upon a question of the using-declared inheriting constructor yesterday. And after carefully reading the answer as well as the linked standard draft N3337, I found there might be some ...

1
2 3 4 5
9