J
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
{
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;
}
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;
}