linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Errors with ->*
@ 2011-08-02 16:31 Shriramana Sharma
  2011-08-02 16:54 ` Aniruddha Bhattacharyya
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Shriramana Sharma @ 2011-08-02 16:31 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 524 bytes --]

Hello -- long time since I posted on this list.

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

[-- Attachment #2: foo.cpp --]
[-- Type: text/x-c++src, Size: 284 bytes --]

struct mystruct 
{
public:
	int x, y ;
	int * xptr, yptr ;
	mystruct () 
	{ 
		xptr = & x ; 
		yptr = & y ; 
	}
} ;

int main ( void )
{
	mystruct a ;
	mystruct * aptr = & a ;

	a . x = 1 ; 
	a . y = 2 ;

	a .* xptr = 3 ;
	a .* yptr = 4 ;

	aptr ->* xptr = 5 ;
	aptr ->* yptr = 6 ;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Errors with ->*
  2011-08-02 16:31 Errors with ->* Shriramana Sharma
@ 2011-08-02 16:54 ` Aniruddha Bhattacharyya
  2011-08-02 16:55   ` Aniruddha Bhattacharyya
  2011-08-02 17:47 ` Darío Mariani
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Aniruddha Bhattacharyya @ 2011-08-02 16:54 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming

1) a .* xptr = 3 ;
   Above line's syntax is wrong.
 Should be
        *a.xptr = 3;

2) Also declare the structure member variables as
         int *xptr, *yptr ; (otherwise yptr will be of type int, not of int*)


3)  aptr ->* xptr = 5 ;
      is also of wrong syntax.
      Should be
                 *aptr->xptr = 5 ;

Which is equivalent to
               *(aptr->xptr) = 5;



the above code DOES NOT mean this one :

             (*aptr)->xptr   // this one dereference aptr first, then
does nothing to xptr



Thanks.

On Tue, Aug 2, 2011 at 10:01 PM, Shriramana Sharma <samjnaa@gmail.com> wrote:
>
> Hello -- long time since I posted on this list.
>
> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Errors with ->*
  2011-08-02 16:54 ` Aniruddha Bhattacharyya
@ 2011-08-02 16:55   ` Aniruddha Bhattacharyya
  0 siblings, 0 replies; 6+ messages in thread
From: Aniruddha Bhattacharyya @ 2011-08-02 16:55 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming

typo correction :
       (*aptr).xptr = 5;

On Tue, Aug 2, 2011 at 10:24 PM, Aniruddha Bhattacharyya
<aniruddha.aot@gmail.com> wrote:
> 1) a .* xptr = 3 ;
>   Above line's syntax is wrong.
>  Should be
>        *a.xptr = 3;
>
> 2) Also declare the structure member variables as
>         int *xptr, *yptr ; (otherwise yptr will be of type int, not of int*)
>
>
> 3)  aptr ->* xptr = 5 ;
>      is also of wrong syntax.
>      Should be
>                 *aptr->xptr = 5 ;
>
> Which is equivalent to
>               *(aptr->xptr) = 5;
>
>
>
> the above code DOES NOT mean this one :
>
>             (*aptr)->xptr   // this one dereference aptr first, then
> does nothing to xptr
>
>
>
> Thanks.
>
> On Tue, Aug 2, 2011 at 10:01 PM, Shriramana Sharma <samjnaa@gmail.com> wrote:
>>
>> Hello -- long time since I posted on this list.
>>
>> 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
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Errors with ->*
  2011-08-02 16:31 Errors with ->* Shriramana Sharma
  2011-08-02 16:54 ` Aniruddha Bhattacharyya
@ 2011-08-02 17:47 ` Darío Mariani
  2011-08-02 20:52 ` Glynn Clements
  2011-08-03  3:57 ` Shriramana Sharma
  3 siblings, 0 replies; 6+ messages in thread
From: Darío Mariani @ 2011-08-02 17:47 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming

Line 9 should be:
int * xptr, * yptr ;
You forgot a '*'. About the rest, you are using the pointer to members
incorrectly, look at
http://www.google.com/search?q=pointer+to+member for examples and usage.
Darío

On Tue, Aug 2, 2011 at 13:31, Shriramana Sharma <samjnaa@gmail.com> wrote:
> Hello -- long time since I posted on this list.
>
> 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
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Errors with ->*
  2011-08-02 16:31 Errors with ->* Shriramana Sharma
  2011-08-02 16:54 ` Aniruddha Bhattacharyya
  2011-08-02 17:47 ` Darío Mariani
@ 2011-08-02 20:52 ` Glynn Clements
  2011-08-03  3:57 ` Shriramana Sharma
  3 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2011-08-02 20:52 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming


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@gclements.plus.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Errors with ->*
  2011-08-02 16:31 Errors with ->* Shriramana Sharma
                   ` (2 preceding siblings ...)
  2011-08-02 20:52 ` Glynn Clements
@ 2011-08-03  3:57 ` Shriramana Sharma
  3 siblings, 0 replies; 6+ messages in thread
From: Shriramana Sharma @ 2011-08-03  3:57 UTC (permalink / raw)
  To: linux-c-programming

Thanks all for your replies. I now have a better idea of the things.
And Glynn's note about comp.lang.c++ is noted.

-- 
Shriramana Sharma

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-08-03  3:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-02 16:31 Errors with ->* Shriramana Sharma
2011-08-02 16:54 ` Aniruddha Bhattacharyya
2011-08-02 16:55   ` Aniruddha Bhattacharyya
2011-08-02 17:47 ` Darío Mariani
2011-08-02 20:52 ` Glynn Clements
2011-08-03  3:57 ` Shriramana Sharma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).