|
-
April 22nd, 2008, 12:51 PM
#1
Member pointer selector issues
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:
Code:
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:
Code:
*(fooobj->m_something) = 5;
Why is this not working? I'm afraid it's something horribly obvious, so go easy on me
Thanks in advance for help.
--MrDoomMaster
--C++ Game Programmer
Don't forget to rate me if I was helpful!
-
April 22nd, 2008, 01:13 PM
#2
Re: Member pointer selector issues
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
Code:
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;
}
-
April 22nd, 2008, 01:17 PM
#3
Re: Member pointer selector issues
 Originally Posted by GCDEF
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
Code:
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.
--MrDoomMaster
--C++ Game Programmer
Don't forget to rate me if I was helpful!
-
April 22nd, 2008, 01:22 PM
#4
Re: Member pointer selector issues
The dereference operator comes first:
Code:
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:
Code:
struct A
{
void foo() {}
};
int main()
{
void (A::*ptr)() = &A::foo;
A a;
(a.*ptr)();
}
Last edited by Hermit; April 22nd, 2008 at 01:40 PM.
- Alon
-
April 22nd, 2008, 06:31 PM
#5
Re: Member pointer selector issues
 Originally Posted by MrDoomMaster
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:
Code:
*(fooobj->m_something) = 5;
Why is this not working? I'm afraid it's something horribly obvious, so go easy on me
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.
Code:
*(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.
Last edited by kempofighter; April 22nd, 2008 at 07:16 PM.
Reason: simplified
-
April 22nd, 2008, 07:14 PM
#6
Re: Member pointer selector issues
 Originally Posted by MrDoomMaster
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.
Code:
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.
Code:
ATestpm.m_func1();
pTestpm->m_func1();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|