Skip to main content
1 vote
2 answers
196 views

What does the presence of the keyword `virtual` really do in the context of inheritance?

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.6k
5 votes
1 answer
206 views

Why the keyword "virtual" is used in classes on whose it has no effect?

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
534 views

Copying virtual base class results in losing shared_ptr owned object

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.1k
-1 votes
2 answers
110 views

What is the difference between (this->*&A::main)(); and this->A::main(); in C++ when dealing with virtual functions? [duplicate]

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
94 views

c++ static_cast to virtual base in runtime

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

Breaking one leg of diamond inheritance problem: alternatives?

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
63 views

Where to place virtual base class copy assignment operator in derived class copy assignment operator?

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
72 views

How can I inherit from two classes with the same non-virtual grandparent classes?

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
152 views

When should constructor of virtual base class be called?

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
72 views

If I inherit some attributes from father's pure virtual class and want to implement them in main, do I have to redefine them in the inherited class?

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
44 views

passing a variable and its value from one class to another through inheritance c#

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
116 views

Why do shared_ptr and unique_ptr have different behavior when dtors are not virtual?

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
73 views

Mixing Templates, Multiple Inheritance, and non-Default Constructor

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
90 views

virtual multiple inheritance constructor

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
48 views

Create diferent childs struct that expand a function of a parent struct, and pass the child object in another function. C++

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

15 30 50 per page
1
2 3 4 5
29