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 but that's true for classes like the one in your example


@aeva no c++isms is generally how I write c++ anyways. I don't know why I didn't confirm incorrect belief I had earlier (I thought all function calls went through a vtable)

· · Web · 0 · 0 · 1
Sign in to participate in the conversation
Friend Camp

Hometown is adapted from Mastodon, a decentralized social network with no ads, no corporate surveillance, and ethical design.