I was wondering if there is a difference in the running time of accessing data from a Struct and accessing data from an Class in C++. In my current understanding, a Struct is a class but with no member functions. I have some code which runs much more slowly with objects than with structs, which seems to suggest that there is a difference, but this confuses me as my intuition tells me that there shouldnt be a large difference.
-
What do you mean by objects? You mean class? because if you do then there is no difference in C++ between structs and classes except that struct members are public by default – Tomer Arazy Jun 07 '13 at 19:08
-
question confuses me a bit. Isn't struct the same as class except that members in struct are by default public but for class private? What do you mean by Object? Object of class? – taocp Jun 07 '13 at 19:08
-
They are the same with different default access. Maybe you're thinking of objects on the stack vs heap? – Inisheer Jun 07 '13 at 19:09
-
code? what code? can you show us some code behaving this funnily? – Ferenc Deak Jun 07 '13 at 19:10
-
Could you show us the code since this is not the correct behavior – aaronman Jun 07 '13 at 19:10
5 Answers
As per " What are the differences between struct and class in C++? ", quoting from C++ FAQ
struct and class are otherwise functionally equivalent
(aside from minor private/public/protected defaults).
So there's no difference.
I don't know why you would have that problem of different running times.
Structs and Classes, I'm assuming thats what you mean by objects, are virtually the same other than structs defaulting to public and classes defaulting to private.
So to sum it up there should be no difference functionally or speed wise
EDIT: the reason you may be experiencing structs running faster is because people tend to use structs for POD's whereas classes have more functionality, however this is based on style not actual the functionality of structs and classes

- 18,343
- 7
- 63
- 78
It is all the same, whether it is a struct or a class (I assume that it what you mean by object, since any instance is technically an object, be that a primitive, a struct or a class), the compiler does the same offset calculation to access members. There isn't really that much of a difference between the two - they may look different in terms of syntax, but the C++ call someObject.someMethod()
is equivalent to the C version SomeObject_someMethod(&someObject)
which is what a member call really is under the table. But this only concerns member methods, the access speed to members of classes and structs is practically identical.

- 47,916
- 17
- 112
- 190
At runtime execution there is no difference in speed of execution.
you can refer similar threads.

- 1
- 1

- 3,297
- 19
- 28
in C++ there are 3 kind of objects that belong to the class type:
- struct
- class
- unions
they are functionally equivalent to each other, so you should expand more and tell more about your implementation.

- 548
- 5
- 13
-
-
@aschepler that's what the standard says, 3 type of class objects, unions are included, how they works or what they offer is another story, there are differences between structs and classes too, doesn't mean that they belong to the same definition of class object. – user2384250 Jun 07 '13 at 19:30