From: "Steve Graegert" <graegerts@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: Accessing a class' member functions
Date: Sun, 26 Mar 2006 14:46:01 +0000 [thread overview]
Message-ID: <6a00c8d50603260646x72f816cdwb2effa835fd0d3a3@mail.gmail.com> (raw)
In-Reply-To: <200603261809.18686.samjnaa@gmail.com>
On 3/26/06, Shriramana Sharma <samjnaa@gmail.com> wrote:
> Sunday, 26 March 2006 13:45 samaye Steve Graegert alekhiit:
>
> > Please note that static member functions can only access global data
> > or static class data and can not be declared virtual.
>
> So if I want some globally accessible function that is not limited to
> accessing global or static data what should I do?
If you are in a situation like that, you either have poorly designed
the application or you have not fully understood the nature of the
product you're using.
Anyway, if you desperately need this functionality you may want to
write an appropriate accessor method that instantiates the class in
question and returns the non-static member from within a static
context:
--- BEGIN ---
#include <iostream>
using namespace std;
class D {
private:
int num;
public:
D() { num = 3; }
int get_num() {
return num;
}
};
class C {
public:
static int get_num() {
D *d = new D;
return d->get_num();
}
};
int main(void) {
cout << "C::get_num(): " << C::get_num() << endl;
return 0;
}
--- END ---
The example prints 3 on stdout.
\Steve
next prev parent reply other threads:[~2006-03-26 14:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-03-26 4:01 Accessing a class' member functions Shriramana Sharma
2006-03-26 8:15 ` Steve Graegert
2006-03-26 12:39 ` Shriramana Sharma
2006-03-26 14:46 ` Steve Graegert [this message]
2006-03-26 16:09 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6a00c8d50603260646x72f816cdwb2effa835fd0d3a3@mail.gmail.com \
--to=graegerts@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).