Decay to base class [duplicate] - c++

This question already has answers here:
Get base class for a type in class hierarchy
(4 answers)
Closed 3 years ago.
Is there a trait that returns the base class of a specific class, with the assumption that there is no multiple inheritance involved? Basically something like:
struct Base
{
};
struct Derived : public Base
{
};
struct DerivedDerived : public Derived
{
};
static_assert(std::is_same_v<base<DerivedDerived>::type,Derived>);
static_assert(std::is_same_v<base<Derived>::type,Base>);
static_assert(std::is_same_v<base<Base>::type,Base>);
// with levels
static_assert(std::is_same_v<base<0,DerivedDerived>::type,Base>);
static_assert(std::is_same_v<base<1,DerivedDerived>::type,Derived>);
static_assert(std::is_same_v<base<2,DerivedDerived>::type,DerivedDerived>);
static_assert(std::is_same_v<base<0,Derived>::type,Base>);
static_assert(std::is_same_v<base<1,Derived>::type,Derived>);

Nope. You can test whether a given type inherits from a given other type with std::is_base_of, but not ask for the base type outright. That is, until C++ gets static reflection sometime in the future.

Related

How does vtable handle multiple inheritance? [duplicate]

This question already has answers here:
Object layout in case of virtual functions and multiple inheritance
(4 answers)
Closed 4 years ago.
I understand that for single inheritance a pointer to a virtual function table is added to determine what parent class functions to call at runtime.
class Genius {
int IQ;
public:
virtual void brag();
};
class Me : public Genius {
int age;
};
When instantiated, the memory layout of Me should look something like
pointer to Genius vtable
int iq
int age
But what happens in the case of multiple inheritance?
// Assume CoolDude has virtual functions as well
class Me : public Genius, public CoolDude {
int age;
};
What does the memory layout of the Me class look like now? How is multiple inheritance handled?
The class will have 2 pointers to vtables, one to its implementation of Genius and one to its implementation of CoolDude. When casting to a base class, the the returned pointer will differ from the original by the offset of the vtable(and other members) or the base class.

include object inside a class or better derive from that class? [duplicate]

This question already has answers here:
Prefer composition over inheritance?
(35 answers)
Closed 8 years ago.
I'm quite new in C++ and I would like to learn good practices from the beginning, so my question explained with an example is:
Having:
class A
{
int mNumber;
};
If I need to use class A inside class B, what is better?to include an object?
class B
{
A * mpA;
int mColor;
};
Or inherit from Class A?
class B : public A
{
int mColor;
};
Is there any good habit talking in a generally way to do this?
Prefer composition over inheritance - however remember that for each particular situation, the other approach might be better.
Composition is:
class A
{
B b;
};
Inheritence is:
class A : public B
{
};
Use the first when the relationship is "has-a" and the latter when it is "is-a".
Your example is a loose type of composition - if the member is a pointer, it doesn't (necessarily) signify ownership.

Is there a difference in the compiler between struct and class with public members and no constructor [duplicate]

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 years ago.
I'm looking at some legacy code that looks like it was converted over from C to C++ and there are various classes that have public member variables and nothing else:
class sampleClass
public:
int fd;
customType clientHandle;
customType serverHandle;
};
From my understanding struct = class with no functions and public members so is this virtually exactly the same as a struct for practical reasons?
What happens when the code is compiled. Is it compiled down to the exact same "stuff" or do they get compiled differently
It is entirely the same, yes. The only two ways in which structs and classes differ are the default protection of members, and the default inheritance type of base classes.
In C++ only the default accessing differs between struct (public) and class (private).
Difference in struct and class and that is by default members of struct are public, while by default members of class are private.
Even while inheriting from struct, default specifier is public, while for class its private.
You can see my video tutorial on this.

C++ inheritance+overloading [duplicate]

This question already has answers here:
C++ inheritance, base methods hidden
(2 answers)
Closed 8 years ago.
In the following code:
struct X{
void stream(int){}
};
struct Y : public X{
void stream(int, int){}
};
int main()
{
Y y;
y.stream(2);
}
Why X::stream(int) is not inherited?
Or it is hided with Y::stream(int, int). If so, why it hidden, not overridden?
Names in derived classes do indeed hide identical names in base classes. This is deliberate. If the base class changes, you don't suddenly and silently want to see a different overload set in your derived class.
To unhide base names explicitly, add using X::stream; into your derived class.

When defining a derived class, why is the base class marked "public?" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between private, public and protected inheritance in C++
One of the examples in my lecture notes is
class TransportShip : public GameUnit {
int capacity;
public:
...
}
Why do we need the "public" modifier before the name of the base class? What would it mean if it wasn't there?
It would mean that the base class is private.
With a class, the base and all members are private by default. With a struct, the base and all members are public by default.
If the base was private, then only members of the class would be able to access members of the base.