Skip to main content
Filter by
Sorted by
Tagged with
1 vote
2 answers
199 views

There are lots of posts on the internet about virtual inheritance in C++. Typically these posts say something like this: Virtual inheritance solves the Diamond Inheritance Problem Virtual inheritance ...
user2138149's user avatar
  • 18.7k
5 votes
1 answer
207 views

Consider the following C++ code: class A { public: int i; } class B: virtual public A{}; class C: virtual public A{}; class D: public B, public C{}; int main() { D obj; obj.i = 0; ...
M. Saamin Rahman's user avatar
16 votes
1 answer
541 views

A colleague at work showed me this program: #include <iostream> #include <memory> struct A { std::shared_ptr<int> u{ new int }; }; struct B : virtual A {}; struct C : virtual A ...
Fedor's user avatar
  • 24.7k
-1 votes
2 answers
117 views

I'm trying to understand the difference between two ways of calling a function in C++, especially when dealing with virtual functions. Here is a simple code example: #include <iostream> class A ...
Diaos wang's user avatar
1 vote
1 answer
96 views

Suppose we have such code: struct Granny { int g; }; struct Mom : virtual Granny { int m; }; struct Son : Mom { int s; }; int main() { int x; std::cin >> x; Mom* mom = (x ? new Son :...
gh_shark's user avatar
1 vote
1 answer
98 views

I am contributing some pieces to a large software project in C++. Here's a problem I'm running into. We already have in the repository (1) A.h class A : public X, public Y, public Z { ... } (2) B....
Charles's user avatar
  • 41
0 votes
0 answers
64 views

In an old closed-source codebase I came across a comment where the author suggests placing virtual base class copy assignment after those of immediate base classes or that it can result in "...
aackmann's user avatar
2 votes
1 answer
79 views

Suppose I have four classes A, B, C and D: class A { public: float someData; float someFunc(); } class B : public A { public: float someOtherData; float someOtherFunc(); } class C : ...
Erwin Raupach's user avatar
1 vote
2 answers
156 views

Consider the following example: struct Grandpa { Grandpa(int x) {} }; struct Dad : Grandpa { Dad(int y) : Grandpa(15) {} }; struct Son : Dad { Son(int z) : Dad(z) {} }; int main () { ...
blonded04's user avatar
  • 531
0 votes
0 answers
78 views

If I inherit some attributes from my father's pure virtual class and want to implement them in main, do I have to redefine them in the inherited class? This is pure virtual class Father with ...
Mirek's user avatar
  • 31
0 votes
0 answers
48 views

I want to use inheritance when calculating the area and volume of a cone. I have a circle class whose variables I want to use. I don’t understand why the area value is not transferred and calculated ...
Nenavizhu_Sumerki's user avatar
0 votes
1 answer
117 views

So, I know that not making polymorphic classes destructors virtual will lead to undefined behavior, and the correct fix to this is to make them virtual. With that being said, why does shared_ptr "...
josh chatham's user avatar
2 votes
3 answers
75 views

I have a base class that implements some functionality, and a template class that virtually extends it. The base class has a non-default constructor since it requires some configuration data. The ...
Mad Physicist's user avatar
0 votes
2 answers
93 views

I'm working on a C++ code example that uses virtual inheritance and multiple inheritance. In my code, I've noticed that I have to call the constructor of the Base class in each derived class, even ...
mopalinski's user avatar
0 votes
0 answers
51 views

I'm creating diferent Child Structs with a diferent implementation of a function, starting with a base Parent function. I want a general reference to call the function of a Child in another struct as ...
Riellia Hionmet's user avatar
3 votes
1 answer
120 views

#include <iostream> using namespace std; struct A{ virtual void f(){}; int a; char ch; }; struct B : public A{ char d; }; struct C{ double dd; int a; char ch; }; ...
haojie zhou's user avatar
-4 votes
1 answer
97 views

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 ...
Alessandro's user avatar
0 votes
2 answers
52 views

How do I pass the non-static member function eval of object Problem1 obj to the object Solver solver ? #include<iostream> #include<functional> // non-templated class struct Solver{ ...
user avatar
0 votes
2 answers
134 views

I have a class say Employee and a subclass (of Employee) say Manager. There are various methods (and data member) in Employee class related to payroll, employee details, appraisal, etc. Similarly for ...
user265976's user avatar
1 vote
1 answer
137 views

There is a class Base which has a function add that inputs integers from the users and prints their sum. There's another class called Derived which publicly inherits the Base class and it also has a ...
Tanishq Kohli's user avatar
-1 votes
3 answers
230 views

First of all, I know that variadic functions can't be virtual in c++. My question is how to emulate the next "incorrect" example. I want to have a class A with "virtual variadic ...
toxic's user avatar
  • 600
5 votes
3 answers
3k views

Recently I have been trying to make a plugin for an old game, and running into a problem similar to Diamond Inheritance. I have a very reduced example, write as follows: #include <iostream> #...
Hydrogen's user avatar
  • 321
0 votes
1 answer
114 views

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

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; ...
MANAR Oussama's user avatar
1 vote
2 answers
453 views

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:...
conectionist's user avatar
  • 2,984
5 votes
0 answers
43 views

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 ...
user19783152's user avatar
1 vote
2 answers
398 views

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 ...
Cauly's user avatar
  • 540
0 votes
2 answers
85 views

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: ...
Rafael's user avatar
  • 129
1 vote
0 answers
34 views

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:...
Kyle Beyer's user avatar
0 votes
1 answer
58 views

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 ...
Givikap120's user avatar
0 votes
0 answers
41 views

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 ...
navid's user avatar
  • 27
3 votes
0 answers
187 views

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 ...
Patrick Wright's user avatar
4 votes
2 answers
23k views

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 ...
LeoChiappini's user avatar
0 votes
0 answers
217 views

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

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 ...
Amber Elferink's user avatar
0 votes
2 answers
376 views

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 ...
emilioho2020's user avatar
2 votes
2 answers
152 views

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(...
Zhang's user avatar
  • 3,406
1 vote
0 answers
103 views

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 ...
oblivioncth's user avatar
3 votes
0 answers
89 views

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 ...
依奈ちゃん's user avatar
2 votes
1 answer
352 views

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 ...
Naveen Kedilaya's user avatar
3 votes
2 answers
141 views

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= " ...
Vivek Mangal's user avatar
5 votes
2 answers
899 views

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 ...
Matthias's user avatar
  • 12.3k
2 votes
0 answers
308 views

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 ...
kubo's user avatar
  • 231
2 votes
2 answers
127 views

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 ...
YoungOne's user avatar
2 votes
1 answer
231 views

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 ...
Fedor's user avatar
  • 24.7k
1 vote
1 answer
141 views

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 ...
matthesinator's user avatar
1 vote
0 answers
51 views

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 ...
janekb04's user avatar
  • 5,095
2 votes
2 answers
252 views

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 ...
fatdragon's user avatar
  • 2,309
-1 votes
1 answer
55 views

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 ...
Maestro's user avatar
  • 2,572
0 votes
1 answer
123 views

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 ...
Eurus_'s user avatar
  • 39

1
2 3 4 5
9