|
|
Subscribe / Log in / New account

GCC begins move to C++

GCC begins move to C++

Posted Jun 2, 2010 9:38 UTC (Wed) by stijn (subscriber, #570)
In reply to: GCC begins move to C++ by man_ls
Parent article: GCC begins move to C++

Of course data encapsulation is easy in C, but not data and methods encapsulation. That is the whole point of objects: define your structures and the code that manages them close together.

It depends on your expectations I think. I'll say that defining your structures and the code that manages them close together is entirely possible in C, and in fact the prefered way. Not in the way that C++ does it, but taken on its own the statement applies to C. I am refering to bog-standard C by the way, nothing that tries to stuff each and every structure full of method pointers and/or uses a shower of preprocessing magic.


to post comments

GCC begins move to C++

Posted Jun 2, 2010 10:07 UTC (Wed) by dark (guest, #8483) [Link] (4 responses)

That's a good point, and you've put your finger on something that has been bothering me about C++. You see, C is actually better at this than C++. In C, I can declare a structure and its methods in a header file, and then define them close together in an implementation file. But when making a C++ class, I have to define the structure and the methods in separate places: the structure fields (= class members) in a header file and the methods in an implementation file. Unless I just put everything in the header file, but that has its drawbacks.

GCC begins move to C++

Posted Jun 2, 2010 13:24 UTC (Wed) by dgm (subscriber, #49227) [Link]

I had to read your comment three times before understanding (hopefully) what you meant: opaque data types, also known as incomplete types.

GCC begins move to C++

Posted Jun 2, 2010 14:32 UTC (Wed) by mjthayer (guest, #39183) [Link]

> But when making a C++ class, I have to define the structure and the methods in separate places: the structure fields (= class members) in a header file and the methods in an implementation file.

Or you define a virtual parent class with just the public interface in a header file and the actual implementation (including its non-public members) in the implementation file. There is a negligeable price in efficiency of course, which should nearly always be worth paying.

GCC begins move to C++

Posted Jun 2, 2010 18:05 UTC (Wed) by jwakely (subscriber, #60262) [Link]

You can do exactly the same in C++, since it's almost a strict superset, how is C better at it?
There are also other alternatives in C++, with their own pros and cons.

Java advantage

Posted Jun 2, 2010 19:37 UTC (Wed) by man_ls (guest, #15091) [Link]

In this respect Java is definitely better than both: you define attributes and methods on the same file, and you don't need separate declarations.

It has always bothered me that I needed to add something in two places to get an accessible function. With a little perspective header files look like a way to make the preprocessor happier at the cost of developer time. Why C++ didn't fix this little detail is beyond me.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds