Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Over-riding static functions/data

Reply
Thread Tools

Over-riding static functions/data

 
 
John
Guest
Posts: n/a
 
      11-04-2009
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;

}


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-04-2009
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
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      11-04-2009
On Nov 4, 3:17*pm, John <(E-Mail Removed)> 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
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      11-04-2009
[Please do not mail me a copy of your followup]

John <(E-Mail Removed)> spake the secret code
<hcrrds$8vc$(E-Mail Removed)> 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>
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      11-05-2009
Thanks to all who answered and for pointing out my mix-up in terminology
concerning over-riding.

John
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 2 05-26-2005 06:25 PM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
Static is REALLY Static! Paul W ASP .Net 2 05-03-2005 10:12 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM
Static vs. non-static connection Natan ASP .Net 8 05-26-2004 08:21 AM



Advertisments