Jump to content
  • Advertisement

Archived

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

amish1234

structs in C vs C++

This topic is 5292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
if you could have inheritance, constructors and destructors in C, then C == C++ would be true.

Share this post


Link to post
Share on other sites
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
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
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
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
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
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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!