Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Over-riding static functions/data

 
Thread Tools Search this Thread
Old 11-04-2009, 12:17 PM   #1
Default Over-riding static functions/data


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
  Reply With Quote
Old 11-04-2009, 12:48 PM   #2
Victor Bazarov
 
Posts: n/a
Default Re: Over-riding static functions/data
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
  Reply With Quote
Old 11-04-2009, 01:11 PM   #3
Saeed Amrollahi
 
Posts: n/a
Default Re: Over-riding static functions/data
On Nov 4, 3:17*pm, John <gh14...@yahoo.com> 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
  Reply With Quote
Old 11-04-2009, 02:30 PM   #4
Richard
 
Posts: n/a
Default Re: Over-riding static functions/data
[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
  Reply With Quote
Old 11-05-2009, 12:25 PM   #5
John
 
Posts: n/a
Default Re: Over-riding static functions/data
Thanks to all who answered and for pointing out my mix-up in terminology
concerning over-riding.

John


John
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
small problem replying in Agent 4.2 GrandpaChuck Computer Support 26 02-10-2007 04:49 AM
Illegal operation carololine Computer Support 12 07-14-2006 01:27 PM
Setting up WDS network with Static, but still need DHCP on client =?Utf-8?B?V1NuaXBlcw==?= Wireless Networking 3 05-26-2006 09:04 PM
STATIC in Close Program box Kirsty Computer Support 10 11-12-2005 10:05 AM
static arp with wireless Anonymous via the Cypherpunks Tonga Remailer Wireless Networking 0 10-04-2005 06:13 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46