Shriramana Sharma wrote:
> I'm trying to understand the use of ->* but in compiling the attached
> program which I thought was straightforward I am getting the following
> errors:
>
> foo.cpp: In constructor ‘mystruct::mystruct()’:
> foo.cpp:9:12: error: invalid conversion from ‘int*’ to ‘int’
> foo.cpp: In function ‘int main()’:
> foo.cpp:21:7: error: ‘xptr’ was not declared in this scope
> foo.cpp:22:7: error: ‘yptr’ was not declared in this scope
>
> Please help.
>
> --
> Shriramana Sharma
> struct mystruct
> {
> public:
> int x, y ;
> int * xptr, yptr ;
If these are supposed to have pointer-to-member type, the correct
syntax is:
int mystruct::* xptr, mystruct::* yptr;
> mystruct ()
> {
> xptr = & x ;
> yptr = & y ;
> }
There's no point in using pointer-to-member instance variables like
this. They're normally used when you need to identify a specific field
within a class (rather than an instance). If you want to point to a
specific member of a specific instance; you would just use plain
pointers.
> a .* xptr = 3 ;
> a .* yptr = 4 ;
>
> aptr ->* xptr = 5 ;
> aptr ->* yptr = 6 ;
xptr and yptr are members of mystruct, so you need e.g.:
aptr ->* aptr->xptr = 5 ;
aptr ->* aptr->yptr = 6 ;
a .* a.xptr = 3 ;
a .* a.yptr = 4 ;
The whole point about pointer-to-member is that the value is the same
for all instances of the class, so you could just do:
int main ( void )
{
int mystruct::* xptr = &mystruct::x;
int mystruct::* yptr = &mystruct::y;
mystruct a, b;
mystruct *aptr = &a;
a .* xptr = 1 ;
a .* yptr = 2 ;
b .* xptr = 1 ;
b .* yptr = 2 ;
aptr ->* xptr = 1 ;
aptr ->* yptr = 2 ;
}
In any case, given that this isn't specific to Linux, and has nothing
to do with C, there are better places for asking such questions than
the linux-c-programming list (e.g. the comp.lang.c++ newsgroup).
--
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
[Linux Assembler]
[Git]
[Kernel List]
[Fedora Development]
[Fedora Announce]
[Autoconf]
[Yosemite Campsites]
[Yosemite News]
[GCC Help]