Over-riding static functions/data

Discussion in 'C++' started by John, Nov 4, 2009.

  1. John

    John Guest

    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;

    }
     
    John, Nov 4, 2009
    #1
    1. Advertising

  2. 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).

    >
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 4, 2009
    #2
    1. Advertising

  3. On Nov 4, 3:17 pm, John <> wrote:
    > 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
     
    Saeed Amrollahi, Nov 4, 2009
    #3
  4. John

    Richard Guest

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

    John <> spake the secret code
    <hcrrds$8vc$> 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/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
     
    Richard, Nov 4, 2009
    #4
  5. John

    John Guest

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

    John
     
    John, Nov 5, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Borgwardt

    Re: Over-riding equals method dilemma

    Michael Borgwardt, Aug 14, 2004, in forum: Java
    Replies:
    23
    Views:
    729
    Chris Uppal
    Aug 23, 2004
  2. Ian Davies

    Link style over riding another

    Ian Davies, Apr 24, 2006, in forum: HTML
    Replies:
    5
    Views:
    455
    Toby Inkster
    Apr 25, 2006
  3. Brian Salter-Duke

    Over-riding an error

    Brian Salter-Duke, May 2, 2009, in forum: C Programming
    Replies:
    21
    Views:
    2,247
    Richard Bos
    May 9, 2009
  4. Steven D'Aprano

    Inheriting methods but over-riding docstrings

    Steven D'Aprano, Jan 16, 2010, in forum: Python
    Replies:
    16
    Views:
    448
    Gabriel Genellina
    Jan 22, 2010
  5. mk

    Over(joy)riding

    mk, Feb 17, 2010, in forum: Python
    Replies:
    7
    Views:
    274
    Bruno Desthuilliers
    Feb 17, 2010
Loading...

Share This Page