Beginning C++ in FreeBSD

Sergey Zaharchenko doublef at
Thu Apr 22 19:41:06 PDT 2004

On Thu, Apr 22, 2004 at 02:43:11PM -0700,
 Chris Ashlee probably wrote:
> DoubleF wrote:
> >[etc] If you think C++ can be translated to C, just translate this:
> >
> >// one module
> >
> >class	A
> >{
> >public:
> >	A();
> >};
> >
> >A::A()
> >{
> >	/* some code here */
> >}
> >
> >static A a;
> >
> >// another module, which doesn't know about the former's existance
> >
> >int
> >main(void)
> >{
> >	/* some other code here */
> >}
> >
> Based on my (limited) knowledge of C++, the constructor for the object 
> 'a' of type A will be called before main...

Yes, that's the catch...

> So if you were to translate 
> this to C, the other module, which doesn't know about the A module, 
> would have to have a call to the constructor inserted before any other 
> code in main, and 'a' would have to be referenced from the module 
> containing main.
> So it seems that some information would be lost in the translation to C, 
> namely that some details of other modules should not be visible. 
> However, the compiled code for both the C++ program and the C 
> translation could well wind up the same.

You would have to translate a program as a whole, not on a file-per-ile
basis, then. I agree that in the end you will get something compilable
--- but it will have large dependency chains which aren't really
necessary, and you'd have to recompile the main module each time the A
module's interface changes, which is certainly inefficient... 

> Any high-level optimizations 
> that can be done on C++ but not C could probably be done in the 
> translation itself. Remember, C is basically a glorified, portable 
> assembly language, and just about anything can be translated to it - 
> even machine code. It's translating to a more high-level language that's 
> hard.

What you surely can't translate is the A module without the main module.
Suppose I want a shlib to initialize itself when it's loaded. I create a
static object and GCC does the rest of the stuff for me (like creating
an init function visible to the linker). But what would you do in C (of
course, you want it portable, so no init hacks, please...).

Fourth Law of Revision:
	It is usually impractical to worry beforehand about
interferences -- if you have none, someone will make one for you.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url :

More information about the freebsd-chat mailing list