473,837 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structs vs. Classes

Dom
I'm really confused by the difference between a Struct and a Class?
Sometimes, I want just a group of fields to go together. A Class
without methods seems wrong, in that it carries too much overhead (I
think). A Struct seems more appropriate. At least it is what I would
have used in other languages. But since a Struct *can* hold methods,
I wander if I am saving anything. If not, why use it?

May 5 '07 #1
29 2792
Hi,
The main difference is that Classes are by reference, but the structs
are passed by value, like the String type.
So Classes are faster than Structures..

Bye,
Diego

May 5 '07 #2
Dom
Well, fine. But I have to ask, what is the proper use of a Struct?
Would I ever use it instead of a Class?

On May 5, 7:08 pm, Diego Jancic <jan...@gmail.c omwrote:
Hi,
The main difference is that Classes are by reference, but the structs
are passed by value, like the String type.
So Classes are faster than Structures..

Bye,
Diego

May 5 '07 #3
Diego Jancic wrote:
Hi,
The main difference is that Classes are by reference, but the structs
are passed by value, like the String type.
So Classes are faster than Structures..
Eugh.
It depends on what you're doing. Think about why ints are passed by
value, as an extreme example.

Alun Harford
May 5 '07 #4
Dom wrote:
I'm really confused by the difference between a Struct and a Class?
Sometimes, I want just a group of fields to go together. A Class
without methods seems wrong, in that it carries too much overhead (I
think). A Struct seems more appropriate. At least it is what I would
have used in other languages. But since a Struct *can* hold methods,
I wander if I am saving anything. If not, why use it?
Structs is value types. Classes is reference types.

There are some different performance characteristics of
those, but it is a rather complex story.

If you go by the rule of using struct for bundling
data with no methods locally withing methods or at
least within assemblies and then use class for all
other, then you will be OK in 99% of cases.

And when you learn more about value types and
reference types, then you will really know.

Arne

May 6 '07 #5
Diego Jancic wrote:
The main difference is that Classes are by reference, but the structs
are passed by value, like the String type.
Both classes and structs are default passed by value and both
can be passed by reference. Passing a reference type bu value
and passing a value type by reference is not the same.
So Classes are faster than Structures..
Sometimes. Definitely not always.

Arne
May 6 '07 #6
Dom
On May 5, 8:25 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
Both classes and structs are default passed by value
Are you sure about this? From MS help: "When passing a struct to a
method, it is passed by value instead of as a reference."

I think the implication is that classes are passed by reference.


May 6 '07 #7
Dom wrote:
On May 5, 8:25 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
>Both classes and structs are default passed by value

Are you sure about this?
112%
From MS help: "When passing a struct to a
method, it is passed by value instead of as a reference."

I think the implication is that classes are passed by reference.
No.

http://msdn2.microsoft.com/en-us/lib...28(VS.80).aspx

Arne

May 6 '07 #8
Dom
I think the implication is that classes are passed by reference.

No.

http://msdn2.microsoft.com/en-us/lib...28(VS.80).aspx

Arne
Then I'm confused by this from MS help: "This example shows that when
a struct is passed to a method, a copy of the struct is passed, but
when a class instance is passed, a reference is passed."

Maybe it's a question of semantics. Your saying that the variable
itself is passed by value. The example shows that this variable can
not be made to point to a new object, except locally. But the
important point is that (I think) when you pass a struct, the entire
struct is placed on the call stack. If the struct is 5 long words,
then all 5 long words are there. When you pass an object, the single
reference is placed on the call stack, not all the fields. Your point
is just that a copy of the reference is placed there.
May 6 '07 #9
Dom wrote:
>>I think the implication is that classes are passed by reference.
No.

http://msdn2.microsoft.com/en-us/lib...28(VS.80).aspx

Then I'm confused by this from MS help: "This example shows that when
a struct is passed to a method, a copy of the struct is passed, but
when a class instance is passed, a reference is passed."

Maybe it's a question of semantics. Your saying that the variable
itself is passed by value. The example shows that this variable can
not be made to point to a new object, except locally. But the
important point is that (I think) when you pass a struct, the entire
struct is placed on the call stack. If the struct is 5 long words,
then all 5 long words are there. When you pass an object, the single
reference is placed on the call stack, not all the fields. Your point
is just that a copy of the reference is placed there.
Exactly.

Arne

May 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1698
by: Der Andere | last post by:
Two short questions: 1. Is there any implementation of sets in PHP4? 2. Can I define structs (e.g., as in C++) or do I have to use classes instead?? Cheers, Matthias --
18
30275
by: Rick | last post by:
Hi, We all know that Java has classes but how about basic storage objects like structs? C and C++ have Structs, Pascal has Records, Visual Basic has Types etc. How about Java? Greetings, Rick
16
1874
by: Michi Henning | last post by:
Below is a bit of code that creates a delegate. The delegate invokes a member function of a struct. The code compiles, but has surprising behavior: using System; namespace ConsoleApplication1 { public struct SimpleStruct {
5
2922
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say that, a structure definition that includes function pointers only defines the function prototypes to be used with them, but not the actual implementations, whereas in C++, member functions cannot be changed *unless* virtual functions are used, or the
14
3842
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of the struct will describe a person - and about 900 instances (people) will be contained in the collection. Users must be able to search for a specific person by any of the properties (e.g., LastName, FirstName,...
61
3792
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
2
9772
by: S. Lorétan | last post by:
Hello. I have some structs in different namespaces/classes/other structs and I sometime have to check if it contains something or not. myStruct == null doesn't work. I've currently done it by creating a IsNull method in my structs: struct TestStruct {
19
2563
by: desktop | last post by:
There is a lot of info on this topic on google. But in Bjarne Stroustrup 's book page 225 he writes: "The declaration of Date in the previous subsection (declared as a struct) provides a set of functions for manipulating a Date. However, it does not specify that those functions should be the only ones to depend directly on Date ’s representation and the only ones to directly access objects of class Date . This restriction can be...
13
2540
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
0
9844
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10578
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10631
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7813
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4477
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.