structs in C vs C++

Started by
10 comments, last by amish1234 20 years, 1 month ago
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.
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
Advertisement
No.
if you could have inheritance, constructors and destructors in C, then C == C++ would be true.
Well, R2D22U2..
What a relief. I thought there was a tear in the fabric of space-time.

___________________________________________________________
Proceeding on a brutal rampage is the obvious choice.
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
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.
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
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!
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!}

--{You fight like a dairy farmer!}

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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

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?


This topic is closed to new replies.

Advertisement