Web Images Groups Books Scholar Blogs Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Over-riding static functions/data
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John  
View profile  
 More options Nov 4, 11:47 pm
Newsgroups: comp.lang.c++
From: John <gh14...@yahoo.com>
Date: Wed, 04 Nov 2009 07:17:53 -0500
Local: Wed, Nov 4 2009 11:47 pm
Subject: 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;


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor Bazarov  
View profile  
 More options Nov 5, 12:18 am
Newsgroups: comp.lang.c++
From: Victor Bazarov <v.Abaza...@comAcast.net>
Date: Wed, 04 Nov 2009 07:48:42 -0500
Local: Thurs, Nov 5 2009 12:18 am
Subject: 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).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Saeed Amrollahi  
View profile  
 More options Nov 5, 12:41 am
Newsgroups: comp.lang.c++
From: Saeed Amrollahi <amrollahi.sa...@gmail.com>
Date: Wed, 4 Nov 2009 05:11:41 -0800 (PST)
Local: Thurs, Nov 5 2009 12:41 am
Subject: Re: Over-riding static functions/data
On Nov 4, 3:17 pm, John <gh14...@yahoo.com> wrote:

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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard  
View profile  
 More options Nov 5, 2:00 am
Newsgroups: comp.lang.c++
From: legalize+jee...@mail.xmission.com (Richard)
Date: Wed, 4 Nov 2009 14:30:21 +0000 (UTC)
Local: Thurs, Nov 5 2009 2:00 am
Subject: Re: Over-riding static functions/data
[Please do not mail me a copy of your followup]

John <gh14...@yahoo.com> spake the secret code
<hcrrds$8v...@aioe.org> 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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John  
View profile  
 More options Nov 5, 11:55 pm
Newsgroups: comp.lang.c++
From: John <gh14...@yahoo.com>
Date: Thu, 05 Nov 2009 07:25:09 -0500
Local: Thurs, Nov 5 2009 11:55 pm
Subject: Re: Over-riding static functions/data
Thanks to all who answered and for pointing out my mix-up in terminology
concerning over-riding.

John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google