* Accessing a class' member functions
@ 2006-03-26 4:01 Shriramana Sharma
2006-03-26 8:15 ` Steve Graegert
0 siblings, 1 reply; 5+ messages in thread
From: Shriramana Sharma @ 2006-03-26 4:01 UTC (permalink / raw)
To: Linux C Programming List
I need to use a class' member functions without creating an instance of that
class. Is that possible? I have seen someone do this in their C++ library but
it does not work for me, so I am wondering what I did wrong.
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-26 W12-7 UTC+0530
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Accessing a class' member functions
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
0 siblings, 1 reply; 5+ messages in thread
From: Steve Graegert @ 2006-03-26 8:15 UTC (permalink / raw)
To: linux-c-programming
On 3/26/06, Shriramana Sharma <samjnaa@gmail.com> wrote:
> I need to use a class' member functions without creating an instance of that
> class. Is that possible? I have seen someone do this in their C++ library but
> it does not work for me, so I am wondering what I did wrong.
What you've have seen are static member functions. They can be called
withouth creating an instance of the class they are defined in.
Please note that static member functions can only access global data
or static class data and can not be declared virtual. Since stattic
member functions exist without class instances, you cannot use the
implicit this pointer to access non-static members.
To access static members you simply use the class name and append the
member using the scope operator: MyClass::myStaticMember.
Example:
-- BEGIN ---
#include <iostream>
#include <cstdlib>
#include <ctime.h>
class C {
private:
static const int MAX_NUM = 10;
public:
static void get_num () {
srand((unsigned)time(NULL));
return (int)(MAX_NUM * rand() / (RAND_MAX + 1.0));
}
};
int main(void) {
cout << C::get_num() << "\n";
}
--- END ---
\Steve
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Accessing a class' member functions
2006-03-26 8:15 ` Steve Graegert
@ 2006-03-26 12:39 ` Shriramana Sharma
2006-03-26 14:46 ` Steve Graegert
2006-03-26 16:09 ` Glynn Clements
0 siblings, 2 replies; 5+ messages in thread
From: Shriramana Sharma @ 2006-03-26 12:39 UTC (permalink / raw)
To: Linux C Programming List
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?
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-26 W12-7 UTC+0530
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Accessing a class' member functions
2006-03-26 12:39 ` Shriramana Sharma
@ 2006-03-26 14:46 ` Steve Graegert
2006-03-26 16:09 ` Glynn Clements
1 sibling, 0 replies; 5+ messages in thread
From: Steve Graegert @ 2006-03-26 14:46 UTC (permalink / raw)
To: linux-c-programming
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Accessing a class' member functions
2006-03-26 12:39 ` Shriramana Sharma
2006-03-26 14:46 ` Steve Graegert
@ 2006-03-26 16:09 ` Glynn Clements
1 sibling, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2006-03-26 16:09 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> > 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?
Huh? What other data do you want to access? To access the member
variables of an instance, you need to actually /have/ an instance.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2006-03-26 16:09 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2006-03-26 16:09 ` Glynn Clements
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).