By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

why int ** test :: iptr = & test :: i ; is incorrect?

P: n/a
HI,

I have following terrific confusion.

class test
{
public:
int i;
int * j;
int **k;
};

int main()
{

//following is the correct syntax.
int test ::* iptr = & test ::i;
int* test::*iptrptr = & test :: j;
int** test :: * iptrptrptr = & test :: k;
//Ok.But I am tempted to use following syntax.
//int* test :: iptr = & test ::i;
//int** test::iptrptr = & test :: j;
//int*** test :: iptrptrptr = & test :: k;
return 1;
}

so can any one tell me why the following syntax is incorrect
int* test :: iptr = & test ::i;
int** test::iptrptr = & test :: j;
int*** test :: iptrptrptr = & test :: k;

Well, I can use the correct syntax with comfort but somehow I am unable
to convince myself why the incorrect syntax is incorrect.
(The confusion will be over once someone tells me the logical thinking
reason behind this and not the kind of answer that compilers interpret
in that way!!:))

Thanks and Regards,
Yogesh Joshi

Feb 9 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
yp*********@indiatimes.com wrote:
I have following terrific confusion.
[...]
int * test :: blah ;

declares a pointer to an int and gives it the name 'test::blah'. It can
be 'blah' member in the 'test' class. It can be 'blah' object inside the
'test' namespace.

int test :: * blah ;

declares 'blah' variable a pointer to int member of 'test'. These are
just rules of syntax for declaring two different things.
Well, I can use the correct syntax with comfort but [...]


Well, when I am in Japan, I have to drive on the left side of the road.
I can drive on the left side with comfort but somehow I am unable to
convince myself why driving on the right would be incorrect...
Feb 9 '06 #2

P: n/a

Victor Bazarov wrote:
yp*********@indiatimes.com wrote:
I have following terrific confusion.
[...]


int * test :: blah ;

declares a pointer to an int and gives it the name 'test::blah'. It can
be 'blah' member in the 'test' class. It can be 'blah' object inside the
'test' namespace.

int test :: * blah ;

declares 'blah' variable a pointer to int member of 'test'. These are
just rules of syntax for declaring two different things.
Well, I can use the correct syntax with comfort but [...]


Well, when I am in Japan, I have to drive on the left side of the road.
I can drive on the left side with comfort but somehow I am unable to
convince myself why driving on the right would be incorrect...

Gr8 answer..must apprieciate it..

Feb 9 '06 #3

P: n/a
Well, when I am in Japan, I have to drive on the left side of the road.
I can drive on the left side with comfort but somehow I am unable to
convince myself why driving on the right would be incorrect...


good answer..must appricate it..

regards,
Yogesh Joshi

Feb 9 '06 #4

P: n/a
In message <11**********************@g44g2000cwa.googlegroups .com>,
yp*********@indiatimes.com writes
HI,

I have following terrific confusion.

class test
{
public:
int i;
int * j;
int **k;
};

int main()
{

//following is the correct syntax.
int test ::* iptr = & test ::i;
int* test::*iptrptr = & test :: j;
int** test :: * iptrptrptr = & test :: k;
//Ok.But I am tempted to use following syntax.
//int* test :: iptr = & test ::i;
//int** test::iptrptr = & test :: j;
//int*** test :: iptrptrptr = & test :: k;
return 1;
}

so can any one tell me why the following syntax is incorrect
int* test :: iptr = & test ::i;
int** test::iptrptr = & test :: j;
int*** test :: iptrptrptr = & test :: k;

Well, I can use the correct syntax with comfort but somehow I am unable
to convince myself why the incorrect syntax is incorrect.
(The confusion will be over once someone tells me the logical thinking
reason behind this and not the kind of answer that compilers interpret
in that way!!:))


When making sense of a declaration, you have to start from the name and
work outwards, which in this case means right to left. So:
[read the following with fixed-pitch font]

int test :: * iptr means

"iptr" is a
* pointer
:: to member
test of class test
int of type int

"iptr is a pointer-to-member of class test which points to some int
member of the class."

Whereas

int * test :: iptr would mean
"iptr" is a
:: member of
test class test
* pointing to
int an int

IOW, "iptr is a member of class test which points to an int", which
clearly isn't true.

It's doubly confusing here because of course a pointer-to-member isn't
really a pointer. It carries the extra information the compiler needs,
given an object of a class, to locate a particular member within it.

--
Richard Herring
Feb 9 '06 #5

P: n/a
<yp*********@indiatimes.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Victor Bazarov wrote:

<snip>
Well, when I am in Japan, I have to drive on the left side of the road.
I can drive on the left side with comfort but somehow I am unable to
convince myself why driving on the right would be incorrect...


Curriously enough, I was in Scotland a year and a half (I live in the
states) where they also drive on the left. I thought it only bugged me for
a while. But after I was there more than a year we were taking a long trip
out on some country roads, and the driver thought it would be funny to drive
on the correct side for a change, the right side. The funny thing was, I
actually relaxed when he was driving on the wrong side of the road, because
to me in my brain it was the right side. Then he want back to driving on
the left side and I felt myself tense up...
Feb 9 '06 #6

P: n/a
> int test :: * iptr means

"iptr" is a
* pointer
:: to member
test of class test
int of type int


after few searches on google and peeping through the C++ books, I
recollected that

..* and ->*are pointer to member operator used for dereferencing the
pointer...
so if ::* is also some kind of an operator ( like declaration operator
for pointer to member ) then the ambiguity in logical thinking can be
resolved.
But unfortunately ::* is not defined as any operator....

regards,
Yogesh Joshi

Feb 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.