• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Liort

c# (or c++) : struct vs class

9 posts in this topic

Hello all! What are the differences (or better off - advantages / disadvantages) of using struct instead of creating a class? Lets say i have a GameObject entity, and for the sake of this discussion, this entity doesn't have any methods, only fields that it stores. 1. Is struct better off being used than class? 2. Can i inherit from a struct like i do with classes? 3. Performance wise what are the benefits? Lior.
0

Share this post


Link to post
Share on other sites
basically there is 'struct' in C++ for compatibility to C.

the *only* difference is that in a struct all members are public by default, and in a class the members are private, by default.

as for your questions:
1) no
2) yes
3) none

you can write classes that are PODs (see wikipedia on 'plain old datatype). and as a rule of thumb, in C++ you only pay for what you use.

struct X {
int x;
};

// is the same as
class Y {
public:
int x;
};


0

Share this post


Link to post
Share on other sites
In C++ the only difference between a class
and a struct is that it's members are private
by default in a class and public by default
in a struct.

I don't know about C# though, not got around
to play with that one yet.
0

Share this post


Link to post
Share on other sites
C# is distinctly different. Structs are value types and thus copied on assignment. They also have restrictions on them to allow copies to be optimized (amongst other reasons). Classes are reference types, and not copied on assignment. The assigned variable is just another reference to the object.

Your usage of the object will quickly determine which to use. Do you need value or reference semantics?
0

Share this post


Link to post
Share on other sites
Quote:
Original post by greenhybrid
the *only* difference is that in a struct all members are public by default, and in a class the members are private, by default.


Also inheritance is public by default in struct, but private by default using class. Nitpicking really [smile].
0

Share this post


Link to post
Share on other sites
I use XNA (C#).

My question was more aimed at C#, but also wanted to know if there's any differences in struct/clas in C++ and C#.
0

Share this post


Link to post
Share on other sites
For the C# in you...

As has been stated, structs are value types, which means that at compile time, a new copy is created and the new copy is returned from or passed to functions that you may call. Structs may still however be cast to an object, which would make them a reference type for that instance.

Classes are always reference types, which mean there is one copy of each instance and they are passed and returned as reference values always.

The other key things to remember have also been stated already: structs be default are public, have public inheritance, and have public members, and classes by default are private and have private members.

For example:


struct Shape
{
int _id;

Shape(int id)
{
_id = id;
}
}


In this struct _id and Shape(int id) are both public as well as the struct. Now for the class:


class Shape
{
int _id;

Shape(int id)
{
_id = id;
}
}


In this case the class, constructor, and _id field are all private, unless you declare them as public.

1. (Answer) C# is built on classes, though you may find instances when a struct is good to use. The ideal container for C# though is the class. Use classes in C#.

2. (Answer) Obviously you can inherit from a struct, it is not so different from the C++ struct.

3. (Answer) There is no performance difference between the two. The Microsoft .NET framework, Common Language Interface, and Just In Time compiler set takes care of all managed (.NET) code and uses pattern matching to create the most optimal compilation at compile time, then when you run it, it does some more optimizations for you. Now if you choose to make your code unmanaged, then you're left to your ability to program well, but I wouldn't advise going in that direction unless you have a very good reason to do so. Let Microsoft .NET be your friend.
0

Share this post


Link to post
Share on other sites
Quote:

As has been stated, structs are value types, which means that at compile time,

It's not really at compile time, the actual copying occurs at runtime.

Quote:

Structs may still however be cast to an object, which would make them a reference type for that instance.

Yes, and for further clarification this process is known as "boxing" the value type. The inverse process is called "unboxing." Technically it does not make the struct a reference type at all, it places the value of the struct in a reference type, giving an instance the semblance of referential behavior. It is misleading to imply the fundamental type changes, as that is never possible.

Quote:

The other key things to remember have also been stated already: structs be default are public, have public inheritance, and have public members, and classes by default are private and have private members.

This is not the case in C#, it applies only to C++. In C#, any aggregate member without an access specifier is private. There is furthermore no concept of access-qualified inheritance in C#. This renders your following example irrelevant to C#.

As for the OP (in C#):
Quote:

1. Is struct better off being used than class?

It's never better to use struct or class in C# in general. Both are equally useful and have their appropriate contexts; the nature of your intended uses of the object matters in determining if it should be a struct or a class. Typically size is considered as well (since copying a value type involves the copy of all members, there can be a performance consideration there).

Your first focus should be semantics, however. If you intend to pass this object around and modify its state, and have that state reflected elsewhere (e.g., referential semantics), you will likely want to use a class.

Quote:

2. Can i inherit from a struct like i do with classes?

Structs, in C#, are implicitly sealed -- that is, they cannot be derived from. Additionally, they cannot be derived from another type (there are special case exception for type machinery required to support the CLR; you should not in general concern yourself with these). However, they are permitted to implement interfaces.

Quote:

3. Performance wise what are the benefits?

Structs incur a cost when they are copied; that cost is proportional to the size of the struct. Structs, however, exist on the stack unless they are boxed or a reference type member, and as a result do not place (as much) pressure on the garbage collector. Classes, as reference types, are cheaper to copy but must be garbage collected.

In general, the performance considerations are not worth worrying until you have identifier a particular class or struct as a potential performance bottleneck. Their performance characteristics are not necessarily black and white.

And finally, to clarify some points the previous poster raised:
Quote:

1. (Answer) C# is built on classes, though you may find instances when a struct is good to use. The ideal container for C# though is the class. Use classes in C#.

2. (Answer) Obviously you can inherit from a struct, it is not so different from the C++ struct.

3. (Answer) There is no performance difference between the two. The Microsoft .NET framework, Common Language Interface, and Just In Time compiler set takes care of all managed (.NET) code and uses pattern matching to create the most optimal compilation at compile time, then when you run it, it does some more optimizations for you. Now if you choose to make your code unmanaged, then you're left to your ability to program well, but I wouldn't advise going in that direction unless you have a very good reason to do so. Let Microsoft .NET be your friend.

For (1) I would say to prefer classes. For (2) you are wrong as far as C# goes, see above. For (3) you are misguided; there are clear performance implications between class and struct in C#, they are just not simple. The CLR is not permitted to change the fundamental type (class or struct) of a type at all.
0

Share this post


Link to post
Share on other sites
The recommended way to code in C++ is to favor value semantics.
The recommended way to code in C# is to favor reference semantics.

Both languages can do both. Java can only do the later.

I personally prefer value semantics, since it is deterministic, doesn't require a GC, doesn't cause annoying side-effects, etc.
Just be careful not to copy your objects when it's not needed.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0