Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-24-2004, 05:47 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
when to use a struct

ok, so my last question got me thinking .. when is it really appropriate to use a struct vs a class?
__________________
Mike
sde is offline   Reply With Quote
Old 02-24-2004, 08:03 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
it's always a good time to use structs! of course, i program in c most of the day, so i never use classes.

but really, a struct is a class with no private data and no member functions. so, if you are going to be manipulating the object's members directly, and don't need any real functions to handle it, a struct is probably appropriate. structs are also crucial where binary compatibility is required. reading and writing binary data to a file, using sockets and interprocess communication, and accessing hardware directly is very much the domain of structs.

on the other hand, where abstraction is key, code modularity and reuse, encapsulation of functionality, and maintaining interfaces is more important (usually true for large projects involving many developers), classes seems to be the popular thing.
joe_bruin is offline   Reply With Quote
Old 02-24-2004, 08:15 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
great, thanks joe!
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2004, 04:11 PM   #4 (permalink)
pe666o
Registered User
 
pe666o's Avatar
 
Join Date: Apr 2004
Posts: 7
pe666o is on a distinguished road
Send a message via ICQ to pe666o
not much to add to what "joe_bruin" said about it.
In C++ struct and class are all the same.
different name for the same thing.
However if/when you go to C#.NET there is a difference. structs- value types class-reference type
so in C# there is difference when you use structs or classes for your data encapsulation.
pe666o is offline   Reply With Quote
Old 01-29-2008, 01:16 PM   #5 (permalink)
zlatnik
Recruit
 
Join Date: Jan 2008
Posts: 1
zlatnik is on a distinguished road
The slight difference between a struct and a class in C++

There is a difference that neither person who posted so far mentioned- all members of a struct are public by default, and all members of a class are private by default. Right now I'm trying to decide when it is appropriate to use a struct in my code (I'm strictly a C++ programmer)- I guess I am not convinced it is ever a good idea, at least with objects that are written to and read from disk. I'm still undecided for certain though.
zlatnik is offline   Reply With Quote
Old 01-30-2008, 09:25 AM   #6 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 681
DJMaze is on a distinguished road
structures are great for two things:

1. easy understandable replacement for to much function parameters.

2. union data so that it can be accessed in different ways

#1 is also used by M$ a lot in Windows C functions just so that it easy to add additional function parameters without loosing viewability.
Have you ever seen functions with 30 parameters? I DID!
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 08-23-2008, 12:21 PM   #7 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 41
landonmkelsey is on a distinguished road
making sure

Quote:
Originally Posted by zlatnik View Post
There is a difference that neither person who posted so far mentioned- all members of a struct are public by default, and all members of a class are private by default. Right now I'm trying to decide when it is appropriate to use a struct in my code (I'm strictly a C++ programmer)- I guess I am not convinced it is ever a good idea, at least with objects that are written to and read from disk. I'm still undecided for certain though.
members of a struct are public by default and members of a class are private by default!

However a struct can have private members using
Code:
private:
there is no other difference!

As somebody pointed out, in C# structs are significantly different than classes!

Here is a stupid interview question I got once:

"is a class a struct?" answer is yes and no
landonmkelsey is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
. vs -> sde Standard C, C++ 3 02-23-2004 01:21 AM
Wierd Problems.... CoW Standard C, C++ 10 12-04-2003 12:59 PM
delete parts of an array Apodysophilia Java 4 05-07-2003 12:11 PM
more help needed kashif Standard C, C++ 0 04-21-2003 10:30 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


All times are GMT -8. The time now is 05:00 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting