0
Standard type "int" is a: struct ?
This is a challenge question. Why is a int a struct ? you could choose between struct, variable, function or class stuct was given as correct answer.
4 ответов
+ 9
In short, int is a built-in datatype, which is one of the PODs (Plain Old Data). Refer:
https://stackoverflow.com/questions/146452/what-are-pod-types-in-c
+ 4
Struct are "value" types, and classes are "reference" types.
The reason you would make a certain type a struct is because:
-Struct instances cannot be null.
-Struct instance are copied when assigned or passed as an argument.
Making int (and other "simple" types) a struct is good, because it prevents you from modifying values you are not intended to. And also it is a pretty small object, so the overhead of making a copy is also small.
Consider this:
int a = 2;
int b = a;
b++;
If int was a reference type, you would have modified the value of "a" because with a reference type doing
"a=b" means that a and b are the SAME object, but if it's a value type you make a copy and don't modify the original value.
For more info, Google "value vs reference type" and you will learn a lot ;)
+ 1
Such a good explanation from Daniel! 👍
0
I think I get it now.
Is this true.
So a struct is a value type, and because a int is a value type it is also a struct.
And class is a reference type.
I know de differences between reference types and value type.
I have never thought of built-in type as being structs or classes.