structs in C vs C++
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.
if you could have inheritance, constructors and destructors in C, then C == C++ would be true.
What a relief. I thought there was a tear in the fabric of space-time.
___________________________________________________________
Proceeding on a brutal rampage is the obvious choice.
___________________________________________________________
Proceeding on a brutal rampage is the obvious choice.
In Bjarne Stroustrup''s ''The C++ Programming Language''
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:
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!
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!}
Thanks
--{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.
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
Popular Topics
Advertisement