[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3299
  • Last Modified:

Struct examples

Hello

Can you give me some examples where you chose to use structs instead of a class?


Thanks
0
Kokas79
Asked:
Kokas79
3 Solutions
 
Jaime OlivaresCommented:
Struct and classes share almost all funcitonality. I usually use a struct where object will be used like a traditionally C structure, without method with exception of some constructor or destructor (I use T prefix in variable name for them). I use class when object will have "live", that is, some other operations rather than simple construction/destruction (with C prefix).
0
 
Jaime OlivaresCommented:
Sorry, I guess proper word is "life" instead of "live"... Still learning english.....
0
 
Jaime OlivaresCommented:
To be more precise: The unique difference between "class"  and "struct" is that a struct has by default public members and a by default public inheritance, whereas a class has by default private members and private inheritance.

But, I like to think that a struct is "stupid" and a class is "smart", at least I think that was the intention of C++ creator, when defaulting it members to different access type.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
madhurdixitCommented:
In struct, you cannot have members having different access privileges like public, private.
Struct doesn't allow you to add methods encapsulated in the struct definition.

Basically struct is a primitive form of encapsulation and far behind several object oriented construct like class(others are interfaces, beans:-special cases of classes )
0
 
Jaime OlivaresCommented:
>In struct, you cannot have members having different access privileges like public, private.
This is not correct, unless my compilers have a virus..... Try with:

struct TSomeStruct
{
private:
     int somevar1;
public:
     int somevar2;
};

>Struct doesn't allow you to add methods encapsulated in the struct definition.
Do you mean this is not possible? :

struct TSomeStruct
{
private:
     int somevar1;
public:
     int somevar2;

     somefunc(int somearg);
};

This is not correct again.
0
 
madhurdixitCommented:
Thanks jaime_olivares  for clarifying, I would surely try out these things. I've never used struct outside C, looks like struct in C++ are quite rich.
0
 
PaulCaswellCommented:
I would use 'struct' if the data structure is intended for binary communication between processes, e.g. via a disc file. You cannot guarantee the size and position of fields in a class, you can in a struct because it cannot be overloaded.

I agree with all else Jaime has said.

Paul
0
 
Eduard GherguCommented:
A structure is a language facility that allow in this way to the programmer to describe data from the real world. A class goes forward and extend the posibilities for describing the real world not only with data but with the actions that can be executed over data.
Depending on the programming language and how the compiler generates code, can be differences in internal representation of a structure and a class (VB, for example) or not (C++).

So, you can choose a structure if you want to store only data, and a class if you want to create a complex entity with a state(data) and a behaviour (methods).
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now