Re: C++ and the kernel

Chris Friesen (cfriesen@nortelnetworks.com)
Tue, 09 Apr 2002 10:00:25 -0400


"Richard B. Johnson" wrote:
>
> On Tue, 9 Apr 2002, Dr. David Alan Gilbert wrote:
> > There are many places in the kernel that are actually very OO - look at
> > filesystems for example. The super_operations sturcture is in effect a
> > virtual function table.
>
> The file operations structure(s) are structures. They are not object-
> oriented in any way, and they are certainly not virtual. The code that
> manipulates them is quite physical and procedural, well defined, and
> visible to the rest of the kernel.

I disagree. A filesystem has a certain set of semantics. There are numerous
types of filesystems, each of which implements that set of semantics (and
possibly additional ones as well) in a different way. This is a classic example
of a situation where oo style programming can be useful. There are many cases
where C code is in essence implementing virtual functions using function
pointers. C++ allows the compiler to do the hard work of keeping track of the
virtual functions.

> It is quite unlikely that a C++ compiler will make more efficient
> code than a C compiler. In fact, the code generator will likely
> be the same. The C++ compiler will end up generating some preamble
> code as part of the function-calling mechanism, that is not necessary
> in C. This means that it will generate a bit more code.

C++ has tigher constraints on code than C. This can allow a compiler to
generate better code because it has more knowledge about what is going on.

> Making code "cleaner" is a matter of perspective.
>
> class A {
> public: void func(char *st) { cout << st << endl; }
> };
> using A::func;
> A a;
> a.func("Hello World!");
>
> Is not all that clean. In fact, I'm not sure I have it right. It's
> easier and clearer to write puts("Hello World!");

Your example is needlessly complex, and I'm sure you know this. A more
realistic comparison would be:

cout << "Hello World!\n";

Now I don't for a moment think that we should go and convert everything to C++.
But I do think that certain features of the language can be useful, and that
there are cases when OO style programming makes the code easier to read and
understand.

-- 
Chris Friesen                    | MailStop: 043/33/F10  
Nortel Networks                  | work: (613) 765-0557
3500 Carling Avenue              | fax:  (613) 765-2986
Nepean, ON K2H 8E9 Canada        | email: cfriesen@nortelnetworks.com
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/