Re: C++ in kernel (was Re: exception in a device driver)

Khimenko Victor (khim@sch57.msk.ru)
Wed, 13 Jan 1999 05:57:01 +0300 (MSK)


In <003101be3db0$d4d945e0$04c809c0@Fake.Domain.com> Anthony Barbachan (barbacha@Hinako.AMBusiness.com) wrote:

>>In <00e101be3d21$96b03ce0$04c809c0@Fake.Domain.com> Anthony Barbachan
AB> (barbacha@Hinako.AMBusiness.com) wrote:
>>
>>> There is a huge difference in the feel of using classes over structures
AB> and
>>> their loosely associated functions.
>>
>>And since C++ does not have real classes (they are only tricks of compiler
AB> and
>>not exists in compiled program) and still lead to speed loss why use it at
AB> all ?
>>

AB> 1 - It does not lead to speed loss.

It does. Take a look:
-- C++ code --
#include <iostream>

class someclass {
int i;
public:
explicit someclass(int _i) : i(_i) {}
virtual foo() {
std::cout << i << endl;
}
};

void test(someclass& x) {
x.foo();
}

void main (void) {
someclass a(1);
test(a);
}
-- C code --
#include <stdio.h>

struct someclass {
int i;
int (*foo)(struct someclass*);
};

int someclass_deffoo(struct someclass* x) {
printf("%d\n",x->i);
}

/*
* int someclass_otherfoo(struct someclass* x) {
* printf("%d\n",x->i<<1);
* }
*/

void test(struct someclass* x) {
x->foo(x);
}

int main (void) {
struct someclass a={1,someclass_deffoo};
test(&a);
/*
* a.foo=someclass_otherfoo;
* test(&a);
*/
return 0;
}
-- cut --

C++ code has one additional INEVITABLE memory reference. Not much, perhaps but
still this shows that main idea: in C coult realize far more flexible object
model then built-in object model of C++ (note commented lines of C sample :-)

AB> 2 - Your statement about no real classes is baseless at best. The same
AB> could be said about any language or even a C structure. Only an interpited
AB> or partially interpited language could have "real" classes by your apparent
AB> definition.

Not at all. Take Java. Replace bytecode with real native code and all is done :-)
Just to use well-know language for samples. AFAIK latest versions of Oberon
also include support for real classes... Where you could change base class and
got derived class with changed behaviour without recompilation...

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/