c++ - Argument passing standardization -
my c++ project getting huge. in situations i'm passing arguments reference own convenience, in don't. here's example:
struct foo{ foo(int &member){ this->member = &member; } private: int *member; };
i'm using pattern when don't want create 2 instances of int
variable. don't have implement get
or modify
methods manipulate value. instead can change variable without accessing foo object. i'm using different way of managing member variables:
struct foo{ foo(int member){ this->member = member; } void modify_member(){ this->member = 6; } int get_member(){ return this->member; } private: int member; };
i'm not sure whether mixing these 2 methods of managing members in same struct practice. should normalize it? example every function in given struct using "pass value" method?
your first case recipe disaster. you'll end dangling pointers , truck load of undefined behaviour.
your second case poor attempt @ encapsulation. there's no need it. use int
. reduce size of code base.
Comments
Post a Comment