c++ - Assign static variable to non-static variable -
consider base class class , baseb class derived. baseb derived c class. clases inherit non-static variable “y” in case of baseb , c class “y” have same value. resolved situation following code:
class base { protected: int y; virtual void registervalue() { y = 5; } }; class : public base { }; class baseb : public base { protected: static int x; virtual void registervalue() { // process x ... y = x; } }; class c : public baseb { }; int baseb::x = 3; int main() {}
it works right assign static variable non-static variable case?
it's fine language legality perspective, it's little odd.
presumably can't make base::y
static
since interfere behaviour of class a
?
you need aware of fact instances of baseb
share same x
, have different values of y
. intended behaviour?
personally i'd consider making void registervalue()
pure virtual function in base class, , expect derived classes implement method including necessary storage implementation. perhaps necessitates base class function virtual int getregistedvalue() = 0
too?
Comments
Post a Comment