|
-
May 17th, 2013, 01:43 AM
#7
Re: Advice on structured header use
 Originally Posted by moffy
I have been thinking about the differences that result from as you mentioned C++ which is OOP and C which is function based. In C++ it makes good sense to hide internal structures and procedures and only expose the public methods of the class because the "class" encapsulates not only the code but the data.
C++ is not just object oriented; it's multi-paradigm. Procedural and generic programming is also fully supported by C++. There are many C++ libraries that are header-only, notably the Boost libraries. When you make heavy use of templates, the guidelines for header use change. That's why I said the interpretation depends on the programming paradigm being used, rather than the programming language being used.
 Originally Posted by moffy
In C it's a bit different as long as you are not writing a LIB or DLL, in which you only expose the public functions. But in a C project, one for internal use rather than public, you tend to have unrestricted access to all functions and data. For an embedded project this can greatly speed up the code as you are not restricted to a class I/O function. I am probably not expressing this as fully as I would like nor as accurately but I hope you can discern my meaning.
In C, it is also possible to emulate the use of public/private functions by hiding a struct definition from the header file.
Code:
// header file
struct Example;
Example* CreateExample(int a); // emulate c'tor
void ReleaseExample(Example* pThis); // emulate d'tor
int ExampleFoo(Example* pThis, int b); // emulate member function
// .c file
struct Example
{
int a;
};
Example* CreateExample(int a)
{
Example* p = (Example*)malloc(sizeof(Example));
p->a = a;
return p;
}
// etc.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|