Over-riding static functions/data

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;

}
 
V

Victor Bazarov

John said:
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).
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;

}

V
 
S

Saeed Amrollahi

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;



}- 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
 
R

Richard

[Please do not mail me a copy of your followup]

John <[email protected]> spake the secret code
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.
 
J

John

Thanks to all who answered and for pointing out my mix-up in terminology
concerning over-riding.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top