How is enum class similar to enum, or a class? [duplicate] - c++

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

Related

Why does the book c++ primer use the struct keyword to describe classes? [duplicate]

This question already has answers here:
When should you use a class vs a struct in C++? [duplicate]
(27 answers)
Closed 1 year ago.
I have basic knowledge of structures from C, and as far as I'm aware, classes and structs are not exactly the same, but the c++ primer defines a "class" using the struct keyword starting on p. 72-73. Here's a small excerpt with the code:
"Defining the Sales_data Type
Although we can’t yet write our Sales_item class, we can write a more concrete class that groups the same data elements. Our strategy for using this class is that users will be able to access the data elements directly and must implement needed operations for themselves.Because our data structure does not support any operations, we’ll name our version Sales_data to distinguish it from Sales_item. We’ll define our class as follows:"
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
This book is suppose to be an authoritative overview of C++11, so why would they use the keyword struct instead of class to describe a class type?
It's common use to define POD types as struct and data types which contain other members, constructors, methods, etc. as class. They are basically the same, the difference being the members are public by default in a struct and private by default in a class.
The usage in the book is consistent with the above description.

Deriving from an enum in C++ [duplicate]

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.

Typedef With A Class In Another Class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
class A{
public:
A(){ letter = 66; }
void display(void){ cout << "A = " << letter; }
private:
int letter;
};
class B{
public:
typedef A classA;
};
int main(void){
B::classA objA;
objA.display();
return 0;
}
This code runs properly ;however, I cannot understand what we do in this code.
I have never seen something like this before. It seems like class A is a member of class B. If it is so, why do not we use classA through object of class B ?
It seems like class A is a member of class B. If it is so, why do not we use classA through object of class B ?
No. What you have there is a name (a type in this case) declared in class B as classA which stands as a typedef to a class, A. In C++, the name of a class is also a namespace. so the typedef, lets you access a name classA within class B.
I have never seen something like this before.
For your specific example, it may be an overkill. But having type aliases is very useful and you will find it a lot in many class templates codes.
Consider:
template<typename T>
class B{
public:
typedef T classA;
};
Without the typedef above, it would be very difficult to tell what type the above class was instantiated with.
A typical example is how STL algorithms can tell the category of a given iterator by accessing the name iterator_category from the iterator's type via std::iterator_traits
The class definition class B { typedef A classA; } does not define any data member in class B; with typedef, it introduces a new name classA as a synonym for type class A). Because this typedef-name is defined within class B, it is in the namespace of class B, such that you need to refer to this new type as B::classA, which is then equivalent to A. Confer, for example, the C++ standard:
7.1.3 The typedef specifier
(1) Declarations containing the decl-specifier typedef declare identifiers that can be used later for naming. ...
A typedef-name is thus a synonym for another type.
...
BTW: A common way for introducing aliases is also the using-statement:
using classA = A;
(2) A typedef-name can also be introduced by an alias-declaration. The
identifier following the using keyword becomes a typedef-name and the
optional attribute-specifier-seq following the identifier appertains
to that typedef-name. It has the same semantics as if it were
introduced by the typedef specifier. In particular, it does not define
a new type and it shall not appear in the type-id.

difference between classes and structure [duplicate]

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 7 years ago.
I read that the main difference between a class and a structure is that class is reference type and structure is value type.
can anybody explain me what does the value type and reference type means...?
You must be thinking of a different language. In C++, class types are semantically the same whether you introduce them with the class or struct keyword. They are object types (which one might loosely call "value types"), in the sense of being objects with a value representation.
The only difference is that base classes and members are public by default if you use struct, and private if you use class.
Reference types are denoted with & or &&, and can refer to any object or function type, not just classes.
The only difference between classes and structs is that by default members/bases are private to a class but public to a struct.
Now values and references are totally orthogonal concepts in C++ to class/struct, basically meaning instance of a class/struct and handle-to-instance.
In c++, the only differences between a struct and a class is the default member access and default inheritance:
struct A : BaseClassOrStruct { // public inheritance
int member; // public member
}
class A : BaseClassOrStruct { // private inheritance
int member; // private member
}
However, I usually do make a distinction between them: I use a struct to indicate that my objects really are just a collection of data members (that typically have public access) without methods (other than setters and getters).

Forward declaring a typedef of an unnamed struct [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forward declarations of unnamed struct
If I have
typedef struct tagPAGERANGE
{
int iFirstPage;
int iLastPage;
} PAGERANGE;
I can forward declare it that way
struct tagPAGERANGE;
typedef struct tagPAGERANGE PAGERANGE;
But what I have is
typedef struct
{
int iFirstPage;
int iLastPage;
} PAGERANGE;
I'm not sure how I can do it. I only want to hold a pointer to this struct. Right now I'm stuck with either including a rather substantial header, or duplicating the definition of the struct.
It's impossible. You can only declare named structs.
Think about what identifies a struct that doesn't have a name, and how do you tell the compiler that it's that struct you want. If it doesn't have a name, it's identified by its members, so you need to provide members — i.e. define it. Therefore, you can't just declare it — you don't have a luxury of an identifier other than the definition itself.
Since this is used in a C++ code, just get rid of the typedefs altogether, they are unnecessary and bad style in C++.
The real solution is to just use named structs:
struct foo; // forward declaration
struct foo {
// … implementation
};
The typedefs are not useful.