c++ - Converting const struct reference to non-const pointer -
i trying work out how convert const struct reference non-const struct pointer.
i have struct called foo:
struct foo { foo& somefunc() { //do homecoming *this; } };
i have method:
struct bar { foo* foopointer; void anotherfunc(const foo& foo) { // set foopointer foo's pointer } };
i phone call this:
foo foo; bar bar; bar.anotherfunc(foo.somefunc());
but how write anotherfunc()?
void anotherfunc(const foo& foo) { foopointer = &foo; // <-- doesn't work foo still const }
you take straight pointer or non-const reference foo
.
the way you're passing foo
, won't able without const_cast
, feels wrong except in few precise case.
as you're storing pointer non-const foo
, should take parameter non-const also. think interface: struct has function takes foo
const qualification, implying "i peak @ it", function casts , stores in way allows modification. it's bit lying users of code (and first user of code).
c++ pointers struct reference
No comments:
Post a Comment