Discussion:
Over-riding static functions/data
(too old to reply)
John
2009-11-04 12:17:53 UTC
Permalink
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; }
};


===== a.cpp =====
#include <iostream>
#include "a.hpp"

std::string A::m_name = "a";
std::string B::m_name = "b";

int main()
{
A aa;
B bb;
std::cout << aa.m_name << std::endl;
std::cout << bb.m_name << std::endl;
std::cout << aa.GetName() << std::endl;
std::cout << bb.GetName() << std::endl;
return 0;

}
Victor Bazarov
2009-11-04 12:48:42 UTC
Permalink
Post by John
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.
Post by John
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).
Post by John
Thanks,
John
===== a.hpp =====
#include<string>
class A
{
static std::string m_name;
static std::string GetName() { return m_name; }
};
class B : public A
{
static std::string m_name;
static std::string GetName() { return m_name; }
};
===== a.cpp =====
#include <iostream>
#include "a.hpp"
std::string A::m_name = "a";
std::string B::m_name = "b";
int main()
{
A aa;
B bb;
std::cout << aa.m_name << std::endl;
std::cout << bb.m_name << std::endl;
std::cout << aa.GetName() << std::endl;
std::cout << bb.GetName() << std::endl;
return 0;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Saeed Amrollahi
2009-11-04 13:11:41 UTC
Permalink
Post by John
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
{
      static std::string m_name;
      static std::string GetName() { return m_name; }};
class B : public A
{
      static std::string m_name;
      static std::string GetName() { return m_name; }
};
===== a.cpp =====
#include <iostream>
#include "a.hpp"
std::string A::m_name = "a";
std::string B::m_name = "b";
int main()
{
    A aa;
    B bb;
    std::cout << aa.m_name << std::endl;
    std::cout << bb.m_name << std::endl;
    std::cout << aa.GetName() << std::endl;
    std::cout << bb.GetName() << std::endl;
   return 0;
}- Hide quoted text -
- Show quoted text -
Hi John

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.

Regards,
-- Saeed Amrollahi
legalize+ (Richard)
2009-11-04 14:30:21 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by John
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/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
John
2009-11-05 12:25:09 UTC
Permalink
Thanks to all who answered and for pointing out my mix-up in terminology
concerning over-riding.

John

Loading...