Click to See Complete Forum and Search --> : Member pointer selector issues
MrDoomMaster
April 22nd, 2008, 12:51 PM
Hi,
I've never actually used ->* or .* before, and I decided that I would experiment with them just to see if I understand how they work. The code below, however, does not compile:
class MyFoo
{
public:
int* m_something;
};
void FooTest()
{
MyFoo* fooobj = new MyFoo;
fooobj->*m_something = 5;
}
It fails with:
error C2065: 'm_something' : undeclared identifier
I read the C++03 standard on these operators just to make sure I have the concept down. From what I understand, ->* is supposed to be the same as:
*(fooobj->m_something) = 5;
Why is this not working? I'm afraid it's something horribly obvious, so go easy on me :p
Thanks in advance for help.
GCDEF
April 22nd, 2008, 01:13 PM
I've never used ->* before but from the documentation, it looks like you'd have to declare something like the following outside the function definition and outside main.
int MyFoo::*something = &MyFoo::m_Something;
then you could use
fooobj->*something = 5;
I haven't tried it. You may want to check MSDN.
From MSDN
using namespace std;
class Testpm {
public:
void m_func1() { cout << "m_func1\n"; }
int m_num;
};
// Define derived types pmfn and pmd.
// These types are pointers to members m_func1() and
// m_num, respectively.
void (Testpm::*pmfn)() = &Testpm::m_func1;
int Testpm::*pmd = &Testpm::m_num;
int main() {
Testpm ATestpm;
Testpm *pTestpm = new Testpm;
// Access the member function
(ATestpm.*pmfn)();
(pTestpm->*pmfn)(); // Parentheses required since * binds
// less tightly than the function call.
// Access the member data
ATestpm.*pmd = 1;
pTestpm->*pmd = 2;
cout << ATestpm.*pmd << endl
<< pTestpm->*pmd << endl;
delete pTestpm;
}
MrDoomMaster
April 22nd, 2008, 01:17 PM
I've never used ->* before but from the documentation, it looks like you'd have to declare something like the following outside the function definition and outside main.
int MyFoo::*something = &MyFoo::m_Something;
then you could use
fooobj->*something = 5;
I haven't tried it. You may want to check MSDN.
From MSDN
using namespace std;
class Testpm {
public:
void m_func1() { cout << "m_func1\n"; }
int m_num;
};
// Define derived types pmfn and pmd.
// These types are pointers to members m_func1() and
// m_num, respectively.
void (Testpm::*pmfn)() = &Testpm::m_func1;
int Testpm::*pmd = &Testpm::m_num;
int main() {
Testpm ATestpm;
Testpm *pTestpm = new Testpm;
// Access the member function
(ATestpm.*pmfn)();
(pTestpm->*pmfn)(); // Parentheses required since * binds
// less tightly than the function call.
// Access the member data
ATestpm.*pmd = 1;
pTestpm->*pmd = 2;
cout << ATestpm.*pmd << endl
<< pTestpm->*pmd << endl;
delete pTestpm;
}
That doesn't make sense. You should be able to use the member as-is. It seems odd to have to define your non-static members as if they were static.
Hermit
April 22nd, 2008, 01:22 PM
The dereference operator comes first:
class MyFoo
{
public:
int* m_something;
};
void FooTest()
{
MyFoo* fooobj = new MyFoo;
*fooobj->m_something = 5;
}
Though I always use extra parentheses.
EDIT: I believe ->* and .* are only used to invoke a pointer to a member function with a particular object. I don't think you'd ever use these operators to dereference a normal pointer member. So:
struct A
{
void foo() {}
};
int main()
{
void (A::*ptr)() = &A::foo;
A a;
(a.*ptr)();
}
kempofighter
April 22nd, 2008, 06:31 PM
I read the C++03 standard on these operators just to make sure I have the concept down. From what I understand, ->* is supposed to be the same as:
*(fooobj->m_something) = 5;
Why is this not working? I'm afraid it's something horribly obvious, so go easy on me :p
Thanks in advance for help.
I think that your understanding of the std is simply incorrect.
m_something is a member variable of the foo class. It has no meaning outside of the class. *m_something would result in undeclared identifier because there is no such variable m_something declared within the global scope.
*(fooobj->m_something) = 5;
The above works because you are first using operator-> to access the public member variable foo::m_something, which is a pointer to an int. Then you use the '*' to dereference that pointer and assign the value of 5 to the contents of that memory location.
kempofighter
April 22nd, 2008, 07:14 PM
That doesn't make sense. You should be able to use the member as-is. It seems odd to have to define your non-static members as if they were static.
Actually, after looking at it that was a really neat example by GCDEF which compiles and executes just fine. Thanks GCDEF.
void (Testpm::*pmfn)() = &Testpm::m_func1;
int Testpm::*pmd = &Testpm::m_num;
You're right. The syntax is odd. pmfn and pmd are not static nor do they exist within testpm's scope. I can see where you'd get confused and think that they look like static member definitions. It's very cryptic looking.
Those entities, which I'm not even sure I'm smart enough to describe accurately, seem like aliases to the actual class members. Those declarations aren't even specific to a particular object since no object had been created yet. Later they are used with the "->* and ->." to access the members via the previously declared aliases.
Since C++ is an OO language, I have never seen deliverable code use that kind of syntax for setting up a pointer to a class member variable nor would I ever recommend doing it. As hermit indicates, I"ve only seen that syntax for setting up member function pointers and even that is something that I come across rarely.
You should be able to use the member as-is.
You can access the public variables as is since they are both public. Just use simpler syntax.
ATestpm.m_func1();
pTestpm->m_func1();
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.