
-----------------------------------
wtd
Sun Nov 26, 2006 2:41 pm

[Tip] Avoid C-style structs in C++
-----------------------------------
Structs in C are simple heterogeneous aggregate data structures.  You can have fields of different types, and give them names.  That's pretty much all you can do.

In C++, structs can certainly be made to look like structs in C, but in reality, they are simply another way of writing classes.

Oh yes, classes.  That means all of the good object-oriented stuff comes along for the ride.

You can have constructors, and member functions, and encapsulation.  It's all there.  And you should have constructors, and probably member functions.

Do not neglect this power.  Make use of it.

-----------------------------------
r.3volved
Sun Nov 26, 2006 4:06 pm


-----------------------------------
In C++ structs are the same as classes except structs are only public and classes can use public, private and protected.

-----------------------------------
wtd
Sun Nov 26, 2006 4:20 pm


-----------------------------------
Wrong.  :)

In a C++ "struct" everything is public by default, but you can still use the same qualifiers to modify visibility.

-----------------------------------
abcdefghijklmnopqrstuvwxy
Wed Jan 17, 2007 2:53 am

RE:[Tip] Avoid C-style structs in C++
-----------------------------------
r.3volved, that sounds like definition provided in Sams Teach Yourself C++ in 21 days practically verbatim, but that was just an oversimplification.  wtd is correct.  And he explained in his first post what the differences are between structs and classes in his initial post...

-----------------------------------
avok23
Sun May 04, 2008 3:46 am

RE:[Tip] Avoid C-style structs in C++
-----------------------------------
structs and classes are the same thing and can even be copied into each other only difference is the default visibility. FACT!! so there is nothing to avoid use them if u feel like it especially if u want to port your code to C.

-----------------------------------
jernst
Sun May 04, 2008 7:57 am

Re: [Tip] Avoid C-style structs in C++
-----------------------------------
I thought that in C when you make a reference to a struct it can initially be NULL but in C++ when you make a reference to an object it automatically creates the default constructor, so that makes them different from one another...assuming i'm correct lol...

-----------------------------------
Saad
Sun May 04, 2008 5:05 pm

Re: RE:[Tip] Avoid C-style structs in C++
-----------------------------------
structs and classes are the same thing and can even be copied into each other only difference is the default visibility. FACT!! so there is nothing to avoid use them if u feel like it especially if u want to port your code to C.

The point was not to not use structures, rather the point was to take the advantage of C++ style structs. Since they are another way of writing classes. You should be taking advantage of constructors, member functions and etc.

-----------------------------------
avok23
Mon May 05, 2008 5:56 pm

RE:[Tip] Avoid C-style structs in C++
-----------------------------------
But structs could be used in casses where u do not want to write accessor functions. It causes no performance problems and comes down to personal preference. The whole reason why c++ is good is beacue of the flexibility it allows u class constraints should be limited to java

-----------------------------------
wtd
Mon May 05, 2008 7:49 pm

RE:[Tip] Avoid C-style structs in C++
-----------------------------------
If you want to make your member variables public, that does not rule out using constructors and member functions.
