Skip to contentSkip to conversation

Accessing a class' member functions

DISCUSSION Shriramana Sharma 3 participants 5 messages Mar 26, 2006

lore
Share & tools
lore
#0 lore raw

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.

Signature · 3 lines
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
#1 lore raw
Quoted 3 lines · Shriramana Sharma

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

#2 lore raw

Sunday, 26 March 2006 13:45 samaye Steve Graegert alekhiit:

Quoted 2 lines

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?

Signature · 3 lines
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
#3 lore raw
Quoted 7 lines · Shriramana Sharma

Sunday, 26 March 2006 13:45 samaye Steve Graegert alekhiit:

Quoted 2 lines

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

#4 lore raw
In reply to Shriramana Sharma
Quoted 5 lines · Shriramana Sharma
Quoted 2 lines

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.

Signature · 1 line
Glynn Clements <[email protected]>