Performance differences between structs and classes

Asked

Viewed 298 times

6

I know the difference between structs and classes is that structs has its public members by default, and that structs belong to C (but nothing prevents use in C++).

In practice when creating an object for classes or a structure, who is the slowest? Which one will occupy the most memory? Who has its fastest access?

1 answer

4


In C++ there is no difference except the one already mentioned in the question, so there is no difference in performance of any type or memory consumption. They could have chosen not to have created the keyword class or could have created it by replacing the struct, if I didn’t want to remain compatible with C. Daria anyway.

  • However, although it has not been asked by the OP there are differences between the two, for example: structs do not have constructors, they are not considered OO so they cannot have inheritance relationships, they cannot have methods, etc.

  • 1

    @Can’t Zorkind? Can you talk about it? And why does it compile? https://ideone.com/LwcDwD And why is it in this documentation? http://en.cppreference.com/w/cpp/language/initializer_list

  • Okay, I was wrong about the builder, for some reason I had it in my head that I couldn’t, but what about the rest? :-)

  • Same thing, it’s all on link that I went through.

  • Ah I had C# in my head about inheritance, sorry https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/struct :-)

  • But anyway the builders in Struct work differently from Class so I’m not totally wrong hahahaha

  • C# allows constructors https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-structs

  • In C++ is identical.

  • i have talked regarding the inheritance in doc I passed it is clear that Struct can not inherit from other Struct in C# :-)

  • Another source confirming everything I said, cannot have default constructor and cannot have inheritance, in C# https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/structs

  • Structs and classes have no difference beyond the default access modifier. In some implementations, there is no difference even pro Linker.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.