Programmer admission 

I always thought classes had some runtime overhead (perf or memory) that wasn't present in structs. Turns out, if you don't use constructor, destructors, or virtual functions they are identical.

I've been using c and cpp and being paid for it for over a decade now.

@jonbro the *only* semantic difference between classes and structs in c++ is class members are private by default and struct members are public by default. otherwise the two forms are totally identical. the only perf overhead is when you use virtual functions because then you need a vtable. the perf overhead is because virtual functions are function pointers. that's all

@aeva the thing I'm wondering about is the difference between a class and a struct that is inside extern "C" {}

will those have effectively the same memory footprint?

I'm guessing that instance.function() is roughly equivalent to function(&instance) as well.

@jonbro i think this is why so many programmers think struct is special, because if you don't use any c++ism you get a "plain old data" layout

@jonbro likewise there's no need to typedef your struct like that, you can just do struct name { ... }, and the form is identical

@aeva huh, I wonder why I do that ... I ran into an issue last night with enums where I needed to do that typedef thing or the compiler complained.

@jonbro i guess that's the c way? typedef is not doing any special work here for structs. the struct { ... } form is just a type definition. you could also do alias name = struct { ... }; if you really wanted to be silly


@jonbro i can't think of any reason why a modern c++ compiler would reject one and not the other, certainly not enums. perhaps you had some other bug that you accidentally fixed when reworking the struct?

Sign in to participate in the conversation

The original server operated by the Mastodon gGmbH non-profit