And that structures will default to public when deriving from :)
– GManNickGJul 14 '09 at 19:15

2

@sth Your right on the difference between struct and class, however I think he's having a compile issue. The issue might be because of a union that is using the struct. You can't have non-trivial constructors in the type you have in a union.
– ChapJul 14 '09 at 20:18

2

@Chap: If he has concrete problems where the general solution doesn't work, it would probably be the best idea to post some code that shows the problem and the compiler errors that are generated. But as general as the question is asked I don't think one can really infer too much about the concrete problem the OP is trying to solve...
– sthJul 14 '09 at 20:36

5

@GMan: Right idea, wrong wording. A struct inherits its base classes publicly by default; there is no change to classes deriving from the struct.
– Ben VoigtApr 5 '13 at 17:37

1

@BenVoigt: Whoa. How'd you find this old comment. :) Yeesh wish I could edit it...even I'm confused at what I wrote. I think I omitted the word "bases" from the end but even that sucks.
– GManNickGApr 5 '13 at 23:40

All the above answers technically answer the asker's question, but just thought I'd point out a case where you might encounter problems.

If you declare your struct like this:

typedef struct{
int x;
foo(){};
} foo;

You will have problems trying to declare a constructor. This is of course because you haven't actually declared a struct named "foo", you've created an anonymous struct and assigned it the alias "foo". This also means you will not be able to use "foo" with a scoping operator in a cpp file:

foo.h:

typedef struct{
int x;
void myFunc(int y);
} foo;

foo.cpp:

//<-- This will not work because the struct "foo" was never declared.
void foo::myFunc(int y)
{
//do something...
}

To fix this, you must either do this:

struct foo{
int x;
foo(){};
};

or this:

typedef struct foo{
int x;
foo(){};
} foo;

Where the latter creates a struct called "foo" and gives it the alias "foo" so you don't have to use the struct keyword when referencing it.

As the other answers mention, a struct is basically treated as a class in C++. This allows you to have a constructor which can be used to initialise the struct with default values. Below, the constructor takes sz and b as arguments, and initializes the other variables to some default values.

Yes structures and classes in C++ are the same except that structures members are public by default whereas classes members are private by default. Anything you can do in a class you should be able to do in a structure.

So that's at least one other difference between a struct and a class. This kind of initialization may not be good OO practice, but it appears all over the place in the legacy WinSDK c++ code that I support. Just so you know...

Struct can have all things as class in c++. As earlier said difference is only that by default C++ member have private access but in struct it is public.But as per programming consideration Use the struct keyword for data-only structures. Use the class keyword for objects that have both data and functions.

In C++, we can declare/define the structure just like class and have the constructors/destructors for the Structures and have variables/functions defined in it.
The only difference is the default scope of the variables/functions defined.
Other than the above difference, mostly you should be able to imitate the functionality of class using structs.

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).