Is it permissible to over-ride a static function or member data in a class? The code below works as I would expect and compiles without error, but I am wondering if it is standard or if it is discouraged practice (and if so, why?).
Thanks, John
===== a.hpp ===== #include<string>
class A { public: static std::string m_name; static std::string GetName() { return m_name; }
};
class B : public A { public: static std::string m_name; static std::string GetName() { return m_name; }
John wrote: > Is it permissible to over-ride a static function or member data in a > class?
Not sure what you mean by 'overriding' here. In C++ the term is used in reference to virtual functions only.
> The code below works as I would expect and compiles without
> error, but I am wondering if it is standard or if it is discouraged > practice (and if so, why?).
It's perfectly fine, from what I can see.
Public member data and public member functions are simply part of the interface of the class. If your interface requirements call for having such data and functions, that's what you have to do.
The members like this do not participate in dynamic polymorphism (you can't call 'B's 'GetName' member through a pointer to 'A', even if you originally create the object as a 'B'), that is achieved through virtual functions. Static data and functions can, of course, participate in "static polymorphism" (when your class is used in a template), and as such are elements of "duck typing" (look it up).
> Is it permissible to over-ride a static function or member data in a > class? The code below works as I would expect and compiles without > error, but I am wondering if it is standard or if it is discouraged > practice (and if so, why?).
> Thanks, > John
> ===== a.hpp ===== > #include<string>
> class A > { > public: > static std::string m_name; > static std::string GetName() { return m_name; }};
> class B : public A > { > public: > static std::string m_name; > static std::string GetName() { return m_name; }
As far as C++ concerned, the term overriding is used for Virtual Functions. As far as your code shows, you don't override something. Indeed, you define two static members - m_name and GetName() - in derived class (B) again. FYI, you can't override static member functions, because for function overriding you need the function be a member of object rather than just a member of class. static members are members of class.
John <gh14...@yahoo.com> spake the secret code <hcrrds$8v...@aioe.org> thusly:
>Is it permissible to over-ride a static function or member data in a >class? The code below works as I would expect and compiles without >error, but I am wondering if it is standard or if it is discouraged >practice (and if so, why?).
You're not really overriding here. The derived class's definitions hide the base class's definitions. Anyone can still get at the base class definitions by casting the derived object to the base.
In real overriding with virtual functions, if they have a pointer to your derived class and cast it to the base class and call the overridden method on the base, it still calls into the derived class's method. -- "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>