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
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.
Re: structs verses classes? When to use them?
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
Re: structs verses classes? When to use them?
Quote:
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?
Re: structs verses classes? When to use them?
Don't forget default inheritance:
Quote:
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.
Re: structs verses classes? When to use them?
Quote:
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. :blush: ]
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).
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) { }
};
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.
Re: structs verses classes? When to use them?
Quote:
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.
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
Re: structs verses classes? When to use them?
Quote:
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. :)
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.
Re: structs verses classes? When to use them?
Quote:
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"... :confused: :confused: