Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

superpig

::*

This topic is 5851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the ::* operator in C++? It claims to be called pointer to member or something like that, but I just don''t get it...

Share this post


Link to post
Share on other sites
Advertisement
Its the operator for specifying pointer-to-member (as opposed to just pointer-to-function).

Here's some code to illustrate the concept:


    
#include <iostream>

using namespace std;

class C
{
public:
void f()
{
cout << "f()\n";
}
};

int main()
{
typedef void (C::*mem_fun_ptr)(); // 1


mem_fun_ptr ptr = &C::f; // 2


C c; // 3


(c.*ptr)(); // 4

}


Line 1 creates a typedef for pointers to functions of type void C::func(void). Line 2 takes the address of a function matching that type, and assigns it to ptr. Line 3 then creates an instance of class C, since an instance is always necessary when calling a non-static member function. Then Line 4 binds the function pointed at by ptr to c (using the ".*" operator), and invokes the function.

Such concepts are rarely of practical use, but sometimes can be useful in achieving genericity in libraries. You generally should avoid such techniques, unless you are writing a library and you have a proven need to do so. The same goes for non-member function pointers.

[edited by - SabreMan on September 13, 2002 6:25:39 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Like2Byte
It''s actual name is the "Scope Operator."

No it isn''t. It''s the "Member Function Declarator". The Scope Resolution Operator is "::".

Share this post


Link to post
Share on other sites
Ah, ok. I think I get it.

So I still need a pointer to an instance of the object that the function is part of? There's no way of using pointer-to-member as a way of setting up, say, a non-static global callback function?

I thought for a moment I could use a structure like:

struct mptr
{
void *obj;
void ( [uh, something]::*)()
};

but the [uh, something] ain't a template

Edit: grrr, silly > and <...

[edited by - Superpig on September 13, 2002 1:18:07 PM]

Share this post


Link to post
Share on other sites
You mean something like this...


  
#include <iostream>

using namespace std;

template<typename T>
class mptr
{
typedef void (T::*func)();
public:
mptr()
: callback(0)
{}
void setCallback(func f)
{
callback = f;
}
void doCallback()
{
(obj.*callback)();
}
private:
T obj;
func callback;
};

struct C
{
void f()
{
cout << "C::f()\n";
}
};

struct D
{
void g()
{
cout << "D::g()\n";
}
};

int main()
{
mptr<C> mc;
mc.setCallback(&C::f);

mptr<D> md;
md.setCallback(&D::g);

mc.doCallback();
md.doCallback();
}


Obviously, you can. The question is, though, what do you want to achieve that you think requires such a mechanism? Or are you just curious?

Share this post


Link to post
Share on other sites
Ah, but if I want to work with an mptr object, I still need to know what class the function belongs to (so that I can create the template). There''s no way of getting a pointer to a member function and completely disassociating it from the class it is a member of?

Share this post


Link to post
Share on other sites
There is, but it''s an extension (in borland C++ && g++).
As MKH points out, the alternative is to use function
objects that encapsulate the pointer to member function
and the object it is called with. (e.g. boost::function,
boost::mem_fn, or std::mem_fun and std::mem_fun_ref).

Of course it will not work with API functions that expect
a real function pointer as a parameter. This case can only
be handled by the aforementioned language extensions
(read up on BC++ ''closures'' and the C++ language extension
sections of the g++ manual).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!