This question already has answers here:
Base enum class inheritance
(13 answers)
Closed 5 years ago.
Is it possible to derive from an enum, if so how?
For instance:
enum eStandardTypes
{
Type1 = 0,
Type2,
Unknown,
Count,
};
enum eExtendedTypes : eStandardTypes
{
Type3 = eStandardTypes::Count,
Unknown,
Count,
};
No this is not possible, even with enum classes.
Support for inheritance of enum classes was discussed for C++17, but was not incorporated into that standard.
Related
This question already has answers here:
Templates polymorphism
(7 answers)
Closed 1 year ago.
Here is my scenario:
class Container {};
class Papa {};
class Child: public Papa {};
void myFunction(Container<Papa> container);
Now I want to pass two different parameters to myFunction().
int main()
{
Container<Papa> papaContainer;
Container<Child> childContainer;
myFunction(papaContainer);
myFunction(childContainer); // this line is not working
}
apparently I cannot do this.
no suitable user-defined conversion from "Container<Child>" to "Container<Papa>"
What would be the solution for this case? How Container<Papa> can be used as interface here?
This seems similar to this question here: No Suitable User Defined Conversion
although that one is using numeric types. Is there anything in myFunction, or that myFunction returns that would need to differentiate Child from Papa?
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 2 years ago.
When writing C++ code, what is the difference between using class and struct?
class Foo final { };
vs.
struct Bar final { };
When should I prefer one over the other? Is this merely a choice of programming/coding style?
A struct simply defines the layout of a piece of storage.
A class is a dynamically-allocated "thing" which can have methods ... it can do things.
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 answers here:
Struct Constructor in C++?
(16 answers)
Closed 9 years ago.
Do C++ struct also call constructors (default, copy constructors) and destructors just like classes or they follow the C language guidelines for struct? So in the example below, is a default constructor called?
Foo structObject; \\Foo is a struct
Yes, they do. The only difference between struct and class in C++ is in visibility of it's members. Struct has by-default members public, class private.
Effectively, writing
class A {
public:
//// ...
}
is the same as writing
struct A {
//// ...
}
This question already has answers here:
What's an enum class and why should I care?
(4 answers)
Closed 10 years ago.
Is the type enum class a completely separate from a traditional class, or is its implementation similar? How does enum class work? The reason I ask is because I don't understand how it can be similar to both a class and an enum at the same time. I assume an enum class cannot have a constructor, or internal methods?
It's related to an ordinary enum in that it consists of a set of names for constant values. It's related to a class in that the names are all contained within the scope of the type's name. So:
enum my_enum {
first,
second,
third
};
my_enum value = second; // OK; enumeration names are in global scope here
enum class my_class_enum {
fourth,
fifth,
sixth
};
my_class_enum other_value = fourth; // Error
my_class_enum another_value = my_class_enum::fourth; // OK