Questions tagged [struct]
7 questions
183
votes
5 answers
When do you use a struct instead of a class?
What are your rules of thumb for when to use structs vs. classes? I'm thinking of the C# definition of those terms but if your language has similar concepts I'd like to hear your opinion as well.
I tend to use classes for almost everything, and use…

RationalGeek
- 10,077
- 7
- 38
- 56
27
votes
9 answers
Is it a security vulnerability to declare class members as public?
I have always wondered whether public, protected, and private has security implications post compilation.
Hypothetically:
class Foo
{
public:
int m_Foo; // Completely vulnerable and dangerous
protected:
int m_Bar; // Possible attack…

Anon
- 3,565
- 3
- 27
- 45
11
votes
1 answer
Why List.Enumerator is struct?
During my investigations of List enumeration I noticed List.Enumerator is struct. In opposite for instance to System.Array.SZArrayEnumerator or System.SZArrayHelper.SZGenericArrayEnumerator that are classes.
It seems this not typical usage…

Yarl
- 288
- 2
- 13
6
votes
0 answers
Rust and lifetime elision rules for structs?
In the Rust documentation, under the structs section, they give this example of how structs need lifetimes when they contain references:
struct Foo<'a> {
x: &'a i32,
}
because
We need to ensure that any reference to a Foo cannot outlive the…

AxiomaticNexus
- 776
- 2
- 7
- 11
5
votes
3 answers
Struct or class for wrapping an int when 0 isn't a valid value
I have a business object which is basically a wrapper around a value of type int. There are some constraints for the value/object:
Not every value in int's range is valid1
The valid values are not a predefined discrete set, therefore an enum is not…

Bill Tür stands with Ukraine
- 285
- 3
- 12
4
votes
2 answers
Why does C not support direct array assignment?
In C you cannot assign arrays directly.
int array[4] = {1, 2, 3, 4};
int array_prime[4] = array; // Error
At first I thought this might because the C facilities were supposed to be implementable with a single or a few instructions and more…

user16217248
- 1,029
- 1
- 5
- 19
-1
votes
1 answer
Few unusual C/C++ declarations
I came across this Enum and Struct declarations in a project supposedly done by an expert.
The declarations / definitions are little different than what im used to so far.
enum EnumKeys {
KEY_MENU ,
KEY_EXIT ,
KEY_DOWN ,
KEY_UP ,
…

ABCD
- 117
- 3