Featured post
Operator overloading in c (not c++) is it possible? -
this question arises need "convert" std::string char* form.
preferably maintain code possible , includes plenty of "=" initialization/assignment operators.
standard c operators exist basic data types (int, char, float, pointers to, etc...).
is possible overload assignment operator in c? suppose each data type it's own operator messing 1 wouldn't mess others.
anyhow, intrigued me can't find references on how c operators implemented.
cheers
well, examples in order
std::string a_string; a_string = "bla"
or using standard c
char a_char_array[10]; strcpy(a_char_array,"bla");
or
char *a_char_pointer = "bla";
what i'm thinking wa replace these 3 use cases single 1 can (through #define) switch between using char* or std::string.
i guess cares performance vs coding speed has thought implications of using std::string instead of char*. of advantages of using stl offseted (is word!?) lower performance (you can't want right??).
as said, have large project , want see myself changes in using 1 method or other.
my post title purposely misleading didn't want become char* vs string battle, guess can't explain want without more details.
cheers
no, in c overloading of =
not possible, simple reason: defined (almost) types.
you'd have distinguish between assignment , initialization. whereas initialization of arrays possible, assignment not. if strings compile time determined, easy:
char copy[] = { "abcd" }; // initialization, ok copy = "1234"; // assignment, error
a trick overcome problem encapsulate string in struct
typedef struct mystring mystring; struct mystring { char a[24]; }; mystring = { "abcd" }; mystring b = a; mystring c = { 0 }; c = b;
this right thing if care initialize variables indicated above.
- Get link
- X
- Other Apps
Comments
Post a Comment