I use C and struct
s where a struct can have members but not functions. Assume for simplicity that I want to create a struct for strings that I name str
and I want to be able to do str.replace(int i, char c)
where i
is the index of the string and c
is the character to replace the character at position i
. Would this never be possible since structs can't have functions or is there still some way we can implement this behavior and mimic that a struct could have a (simple) function that actually only is the struct copying itself to a new struct and updating its fields, which it could do?
So replace
could be a third member of the struct that points to a new struct that is updated when it is accessed or similar. Could it be done? Or is there something builtin or some theory or paradigm that prevents my intention?
The background is that I'm writing C code and I find myself reinventing functions that I know are library builtins in OOP languages and that OOP would be a good way to manipulate strings and commands.
str.replace(&str, i, c)
anyway. C++ automates the passing of thethis
pointer, of course.