Archived

This topic is now archived and is closed to further replies.

amish1234

structs in C vs C++

Recommended Posts

amish1234    104
I''ve seen structs that have constructors and destructors as well as struct inheritance. Can you do these things in C (as opposed to C++)? ___________________________________________________________ Proceeding on a brutal rampage is the obvious choice.

Share this post


Link to post
Share on other sites
amish1234    104
What a relief. I thought there was a tear in the fabric of space-time.

___________________________________________________________
Proceeding on a brutal rampage is the obvious choice.

Share this post


Link to post
Share on other sites
petewood    819
In Bjarne Stroustrup''s ''The C++ Programming Language''
quote:

struct s {...

is shorthand for

class s { public: ...


So structs can do everything that classes do. They default to public members and public inheritance whereas classes default to private members and private inheritance.

Share this post


Link to post
Share on other sites
Narcist    126
quote:
Original post by petewood
In Bjarne Stroustrup''s ''The C++ Programming Language''
quote:

struct s {...

is shorthand for

class s { public: ...


So structs can do everything that classes do. They default to public members and public inheritance whereas classes default to private members and private inheritance.



But only in C++, in C they cant

Share this post


Link to post
Share on other sites
Mathematix    259
Classes

Only available in C++. Can have public, private and protected member functions and member variables. These are private by default.

Structs

C++

As with Classes, except all members are public by default.

C

Structs are also publice by default but cannot contain member functions - C''s structs can only have member variables.

There you go!

Share this post


Link to post
Share on other sites
Greatwolf    125
I know I saw an article a while back on gamedev.net about how an OOP design can be enforced using C. But for some reason I can''t seem to locate it no matter how much I try searching. Does anyone happen to have a link to the article handy?

Thanks





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
DrPizza    160
quote:
As with Classes, except all members are public by default.

And inheritance is public by default too.

quote:
Structs are also publice by default but cannot contain member functions - C''s structs can only have member variables.


It''s not simply "by default". It''s "for want of any alternative". C has no access control modifiers.

Share this post


Link to post
Share on other sites
vicviper    130

I''ve been using c++ structures with member functions for some time... one thing it puzzles me is:

although you can define constructors and destructors for a structure... what if you don''t define any?

if you do this:

class xxx {};
xxx arrayx[100]; // here, a constructor is called 100 times

struct yyy {};
yyy arrayy[100]; // just a plain sizeof(yyy)*100 memory allocation, or 100 constructor calls also?


Share this post


Link to post
Share on other sites
marijnh    182
If you give no constructor or destructor a default one will be provided. All this does is call the default constructor for all member variables (i.e. it does nothing if there are only primitive member variables).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by vicviper

I''ve been using c++ structures with member functions for some time... one thing it puzzles me is:

although you can define constructors and destructors for a structure... what if you don''t define any?

if you do this:

class xxx {};
xxx arrayx[100]; // here, a constructor is called 100 times

struct yyy {};
yyy arrayy[100]; // just a plain sizeof(yyy)*100 memory allocation, or 100 constructor calls also?






If you don''t provide constructor/destructor for your classes or structures, compiler generates them. In that case those methods will do absolute nothing and they are inlined.

so:

struct myStructure
{
int x;
};

equals to:

struct myStructure2
{
myStructure2() {}
~myStructure2() {}
int x;
};

PS. There are also couple of other methods/operators that are automaticly generated by the compiler if you don''t provide them.


Share this post


Link to post
Share on other sites