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.
Related
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.
This question already has an answer here:
Does attribute specifier sequence inherit?
(1 answer)
Closed 4 years ago.
I think it's not really a duplicate of Are function attributes inherited?, because I'm wondering about classes, not member functions :
struct [[nodiscard]] error {};
struct critical_error : error {};
critical_error foo();
int main() {
foo(); // no warning.
}
It seems that the [[nodiscard]] attribute is not inherited here. Is it the same for all type-attributes?
They aren't, as you asserted yourself. The standard is explicit in what exactly is inherited from a base class to a derived one:
10.6 Derived classes [class.derived]
2 [...] Unless redeclared in the derived class, members of a base class are also considered to be members of the derived class.
Members of a base class other than constructors are said to be inherited by the derived class.
Constructors of a base class can also be inherited as described in [namespace.udecl].
Inherited members can be referred to in expressions in the same manner as other members of the derived class, unless their names are hidden or ambiguous ([class.member.lookup]).
For the sake of completeness: There is also no wording about inheritance in the specific section about attributes.
Basically: an attribute is not a member of the class or a constructor, so it can't be inherited.
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.
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 6 years ago.
Suppose i have a header which contain these two classes
class A:public class B{
// code
};
class B
{
protected:
A a_object;
};
when the compiler compiles this include file,when it comes to Class A ,it sees class A inherits from B but it doesn't reach to Class B definition.So it gives an error. and if i reverse the order of both classes,it gives error due to a_object as it doesn't see Class A definition.
So how to solve this problem? and assume that i restricted to this include file to have class A and B definitions.
Thanks
Here is a simplified version of your problem:
class X {
X x;
};
A class cannot embed an object of its own type.
Specifically, your class B embeds an object of type A, which by inheritance is also of type B.
If you really have to have a hierarchy like that you can do something like the following.
class A;
class B
{
protected:
A* a_object;
};
class A: public B {
// code
};
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.