( djszapi | 2014. 05. 06., k – 00:22 )

En eleve nem ertem, hogy miert hivnal C++ kodot C-bol egy uj szoftverre. Ha csak az az indok, hogy a C-t lehet hivni sok nyelvbol, ezert irsz C kodot, akar C++ kod hivasaval, akkor szamomra az egesz szisztema gyanus. C++-bol bindingolhatsz igazabol mindegyik dominans nyelvre relative konnyeden. Ha ezt megteheted, akkor mi szukseg a C-re? Szerintem egy C interface kellemetlen tud lenni... Peldak:

===== C =====

#define foomultiply(a, b) (a)*(b)

void swap(int *a, int *b) {int temp = *a; *a = *b; *b = temp; }

enum FooBar {
OneFooBar,
TwoFooBar,
InvalidFooBar;
};

enum FooBaz {
OneFooBaz,
TwoFooBaz,
InvalidFooBaz,
};

void do_foo(void);
void do_foo_integer(int);
void do_foo_bool(bool); /* not standard until C99! */
void do_foo_string_and_float(char*, float);
void do_foo_function_pointer(void*);

void bool do_foo_error_occured();

void print_hello(hello);
void print_world(world);

===== C++ =====

namepace Foo { /* can be class, too, obviously */

template < class T >
T multiply(T a, T b) { return a*b; }

// just use std::swap or reference
void swap(int &a, int &b) {int temp = a; a = b; b = temp; }

class Enum Bar {
One,
Two,
Invalid
};

class Enum Baz {
One,
Two,
Invalid
};
void do();
void do(int);
void do(bool);
void do(string, float);
void do(function);

// No need for explicit error check in each part of the stack; just catch the exception

operator<<(std::ostream& os, const hello&);
operator<<(std::ostream& os, const world&);
};

Es akkor raadasul nem is a global teruletet szorod tele. De ez csak par pelda a gaz C interface elvek kozul. Direkt nem is emlitettem a classokat, mert szamomra nem az a varazsa a C++-nak. Szerintem a rust ezt jobban megoldotta trait-tekkel, mint a C++ class-ok. Na mindegy. En nagy ivben kerulom a C interface-ket, amennyire csak lehet.