Structures to Classes

Pages: 12
Hey,

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?
Last edited on
In C++ struct and class are exactly the same, except class defaults to private and struct defaults to public.

Even comparing C to C++, I highly doubt there is a difference.
In C structs can't contain functions (function pointers, yes, but not functions).

Also, in C objects are created like this: struct foo bar; whereas in C++ you do this: foo bar;

Edit: also, wrong forum.
Last edited on
I can positively confirm that there is no performance difference between classes and
structs.
Maybe the author meant that, if you're writing a class such as this:
1
2
3
4
5
6
7
8
9
class A{
	T1 member1;
	T2 member2;
public:
	T1 get_member1();
	void set_member1(T1);
	T2 get_member2();
	void set_member1(T2);
};
, it's more efficient to just get it over with:
1
2
3
4
struct A{
	T1 member1;
	T2 member2;
};
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.

-Albatross

EDIT: 600 posts and counting.
I wonder what's the point of having a struct
Duh. Backwards compatibility with C.

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.
Duh. Backwards compatibility with C.


Last time I checked, structs in C couldn't contain functions.

-Albatros
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.
I won!
Well. Then what's the point of having a class?

-Albatross
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.
Last edited on
*sigh*

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++.

-Albatross
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.


So we were just cutting corners.

-Albatross
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.
Yep. We were just cutting corners. Okay, maybe a bit more than corners, however...

I wonder when C+=2 will come out...

-Albatross
I thought someone wrote a new article.
closed account (S6k9GNh0)
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.
Last edited on
Pages: 12