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).
Related
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.
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:
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.
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Differences between structs and classes?
(3 answers)
Closed 9 years ago.
For example, what is the difference between:
strucut Nodo{
Nodo *siguiente,*anterior;
char* Nombre,Curso;
long,carnet;
Nodo(){
siguiente=anterior=NULL;
}
and:
class Nodo{
public:
Nodo *siguiente,*anterior;
char* Nombre,Curso;
long carnet;
Nodo(){
siguiente=anterior=NULL;
}
A difference that i think i saw was that in class you must put public, or in other hand all the statements will be private, but i can´t see any important difference, or any crusial difference. There are some important aspect that i dont know?
The only difference between a class and a struct is the default access specifier. A class's members are private by default, whereas a struct's members are public by default. This also means that class inheritance is private by default and struct inheritance is public by default.
As described by the standard:
Members of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.
And for inheritance:
In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Do static members of a class occupy memory if no object of that class is created?
Memory Allocation of Static Members in a Class
"A class is not considered defined untill its class body is complete, a class can not have data members of its own type. A class can have data members that are pointers/reference to its own type."
C++ Primer (Lippman Lajoie)
Makes sense.
But why is this allowed then ?
class justAClass
{
public :
justAClass();
private :
static justAClass justAMember;
}
For pointers it is understandable. But how will this above thing work ? How will i ever decide the size for object of such a class ? Isnt it a recursive case (with no base condition) to have a member of its own type, even if it is static ?
The reason for class can't have data members of its own type is the compiler must know the size of class object.
For example, one class is a local variable in function, the compiler can handle the stack only it knows the class size.
For your case, the static class member doesn't reside in class object, so has no impact to size of class object. It's OK.
Formally, the distinction is that the declaration of a static member in a class is not a definition. You must provide a definition elsewhere (exactly once), and the compiler doesn't need to know the size until it encounters the definition. Static members do not impact on the size of the class itself. (In many ways, the static member declaration in the class is very much like an extern non-member declaration.)