430 questions
1
vote
2
answers
199
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 ...
5
votes
1
answer
207
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;
...
16
votes
1
answer
541
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 ...
-1
votes
2
answers
117
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 ...
1
vote
1
answer
96
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 :...
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....
0
votes
0
answers
64
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 "...
2
votes
1
answer
79
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 : ...
1
vote
2
answers
156
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 () {
...
0
votes
0
answers
78
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 ...
0
votes
0
answers
48
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 ...
0
votes
1
answer
117
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 "...
2
votes
3
answers
75
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 ...
0
votes
2
answers
93
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 ...
0
votes
0
answers
51
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 ...
3
votes
1
answer
120
views
Why are the sizes of struct b and struct d different?
#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;
};
...
-4
votes
1
answer
97
views
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 ...
0
votes
2
answers
52
views
pass non-static member function with neither virtual inheritance nor templates [duplicate]
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{
...
0
votes
2
answers
134
views
How to make use of a fixed hierarchy of classes for various similar entities, like employee-Manager for company 1,2,3
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 ...
1
vote
1
answer
137
views
How do I call the function of the base class to show runtime polymorphism?
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 ...
-1
votes
3
answers
230
views
How to emulate override of parts of "virtual variadic functions"?
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 ...
5
votes
3
answers
3k
views
What is happening under the hood of virtual inheritance?
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>
#...
0
votes
1
answer
114
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
62
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
453
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:...
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
398
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 ...
0
votes
2
answers
85
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:
...
1
vote
0
answers
34
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
58
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
41
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 ...
3
votes
0
answers
187
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 ...
4
votes
2
answers
23k
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
217
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
155
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
376
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
152
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(...
1
vote
0
answers
103
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
89
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
352
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
141
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= " ...
5
votes
2
answers
899
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 ...
2
votes
0
answers
308
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 ...
2
votes
2
answers
127
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
231
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 ...
1
vote
1
answer
141
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
51
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 ...
2
votes
2
answers
252
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 ...
-1
votes
1
answer
55
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 ...
0
votes
1
answer
123
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 ...