I have just read that classes are more efficient than structures if their members are being accessed often in your code. Is this true or not? Does changing structures to classes have a real noticable impact on performance?
I wonder what's the point of having a struct if it's the exact same object as a class except that it defaults to public. And I also wonder why we almost never see functions in structs on this forum when we see a struct.
Detractors say C++ would be a better language if it wasn't compatible with C, but those people never had to *really* interact with C. You can't fully appreciate the compatibility until you do.
Yeah, but if they removed structs then all the C code that uses them would have to be rewritten. Whether C structs can do everything C++ structs can do is irrelevant to backwards compatibility; what's important is that C++ structs can do everything C structs can do.
I said "backwards", not "upward". Almost every valid C struct is a valid C++ struct, but many valid C++ structs are not valid C structs. That's what it means to be a superset.
Classes are the real OOP concept. Structs are modified classes that are there merely for compatibility.
If you look at it now, yes, one of the two seems useless. You have to put yourself in the shoes of the designer to understand why both of them exist in the same language. Many language features in many languages are historical accidents. For example, & only has a lower precedence than == to ease the transition from a protolanguage of C. Either K or R (I can't remember right now) later realized it would have been better to give it a higher precedence to allow the very common idiom flag&pattern==pattern without cumbersome parentheses.
Okay. They kept structs for backwards compatibility with C. I can understand that. And classes were added because a) the name sounded better and b) according to you, they're the real OOP concept.
However, why add the capability to contain functions to structs? It's irrelevant what shoes you put yourself in, there's a few redundant features in C++.
It's irrelevant what shoes you put yourself in, there's a few redundant features in C++.
Did I ever imply that wasn't the case?
However, why add the capability to contain functions to structs?
Because that simplifies implementation. Instead of having two different concepts that have to be implemented differently, you have a single concept, instances of which are spawned in the symbol table differently depending on which keyword was used. The only minor drawback is that they had to come up with the notion of POD types, or a lot of C code could break.
Because that simplifies implementation. Instead of having two different concepts that have to be implemented differently, you have a single concept, instances of which are spawned in the symbol table differently depending on which keyword was used.
Features that would have made the language significantly better, like defined name mangling, were removed because of the ridiculous complexity they added. Worthless features like true C structs, which really are just crippled classes, would have never made it.
Its not just cutting corners Albatross. Its simplifying something to be of easier use. Why add more rules to an already oversize rule-set? People often prefer that if you use a struct, you not use functions like in C, and if you class, you add functions (and are often required to add functions) to the class. No one forces you to put functions into your struct but it gives you the ability to do so because C++ is a superset which adds functionality.