-
May 23rd, 2007, 08:06 AM
#1
structs verses classes? When to use them?
Hi
When is the best time to use structs over classes ?
Or should one always use classes for object oriented programming ?
Is structs such a relic of procedural programming ?
I notice it doesn't seem like the concept exists in java. In java everything appears to just be a class - unless I'm mistaken.
stephen
-
May 23rd, 2007, 08:14 AM
#2
Re: structs verses classes? When to use them?
[see clarification below...]
In C++ the ONLY difference between a class and a struct is the default visibility after the opening brace:
Code:
struct myStruct
{
// Items placed right here are public
}
class myClass
{
// Items placed right here are private
}
Anything beyond this is simply a matter of style, and consistancy matter most.
Last edited by TheCPUWizard; May 23rd, 2007 at 01:52 PM.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 23rd, 2007, 08:51 AM
#3
Re: structs verses classes? When to use them?
Last edited by blueday54555; May 24th, 2007 at 02:46 AM.
-
May 23rd, 2007, 09:17 AM
#4
Re: structs verses classes? When to use them?
Any links to C# material just causes more confusion.
In C++, there is no difference between a struct and a class except for what CPUWizard mentioned in his post.
Regards,
Paul McKenzie
-
May 23rd, 2007, 09:21 AM
#5
Re: structs verses classes? When to use them?
Originally Posted by blueday54555
Agreeing with Paul, this links are more likely to cause confusion than to help. The C# [and other managed languages] rules are completely different. And there it DOES matter. But that is for another forum...
<blueday54555> would you consider going back and editing your post, so as not to confuse potential readers?
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 23rd, 2007, 01:22 PM
#6
Re: structs verses classes? When to use them?
Don't forget default inheritance:
Originally Posted by TheCPUWizard
In C++ the ONLY difference between a class and a struct is the default visibility after the opening brace:
Code:
struct myStruct : myBase // inherited publically
{
// Items placed right here are public
}
class myClass : myBase // inherited privately
{
// Items placed right here are private
}
Anything beyond this is simply a matter of style, and consistancy matter most.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
May 23rd, 2007, 01:53 PM
#7
Re: structs verses classes? When to use them?
Originally Posted by Graham
Don't forget default inheritance:
Graham is correct, the difference in default visibility effects BOTH locations.....
1) The base class (if any) visibility
2) Member visibility directly after the opening brace.
[I tend to forget the first, since I tend to always explicitly declare base class visibility. ]
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 24th, 2007, 01:13 AM
#8
Re: structs verses classes? When to use them?
Well, another one but not actually in relation to structures or classes but just the keywords - in specifying template type parameters, you cannot use struct in place of class (you can use typename, though).
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 24th, 2007, 01:26 AM
#9
Re: structs verses classes? When to use them?
In addition to the good answers so far. As for the style, you might consider using structures for simple structure-like objects, possibly without very many functions or inheritance or virtual functions, whereby classes could be used for extended objects with several or many functions, lots of data, possible inheritance, etc.
It is hard to define a single all-encompasing style in this matter, but the following samples might be indicative of a kind of style.
Sincerely, Chris.
Code:
struct SimpleDataAddressPair
{
unsigned int addr;
unsigned char data;
SimpleDataAddressPair(unsigned int a = 0u, unsigned char d = 0u) : addr(a), data(d) { }
};
class ExtensiveObject
{
private:
int m1;
int m2;
int m3;
int m4;
public:
ExtensiveObject() : m1(0), m2(0), m3(0), m4(0) { }
virtual ~ExtensiveObject() { }
public:
virtual void do_this(void) { }
virtual void do_that(void) { }
virtual void do_the_other_thing(void) { }
};
Last edited by dude_1967; May 24th, 2007 at 01:27 AM.
Reason: corrections...
You're gonna go blind staring into that box all day.
-
May 24th, 2007, 01:40 AM
#10
Re: structs verses classes? When to use them?
dude_1967, you added a constructor to the POD struct - that is not necessary - POD structs can be initialized using the initializer list and don't necessarily need a constructor unless there in a non-POD type included - which would itself make the struct non-POD and it is best (style) to write them as classes.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 24th, 2007, 04:35 AM
#11
Re: structs verses classes? When to use them?
Originally Posted by exterminator
dude_1967, you added a constructor to the POD struct - that is not necessary - POD structs can be initialized using the initializer list and don't necessarily need a constructor...
Oh. Thank you for pointing that out, exterminator. Somewhere along the lines I seem to have forgotten about the initializer list.
Sincerely, Chris.
You're gonna go blind staring into that box all day.
-
May 24th, 2007, 04:58 AM
#12
Re: structs verses classes? When to use them?
@exterminator
What is meant by initializer list. Will you give an example for it, will it be like this ?
Code:
struct SimpleDataAddressPair var = { 65, 'A' };
Thanks
-
May 24th, 2007, 05:59 AM
#13
Re: structs verses classes? When to use them?
Originally Posted by irs_b
@exterminator
What is meant by initializer list. Will you give an example for it, will it be like this ?
Code:
struct SimpleDataAddressPair var = { 65, 'A' };
Yes.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 24th, 2007, 07:06 AM
#14
Re: structs verses classes? When to use them?
Function object always can be implemented as a struct.
For example:
struct Op
{
bool operator()(const Foo& foo1, const Foo& foo2) const;
};
Because there's always no variables in a function object.
-
May 24th, 2007, 07:16 AM
#15
Re: structs verses classes? When to use them?
Originally Posted by sinall
Function object always can be implemented as a struct.
For example:
struct Op
{
bool operator()(const Foo& foo1, const Foo& foo2) const;
};
Because there's always no variables in a function object.
I fail to see why this is any type of "special case"...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|