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

Y

ypjofficial

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
 
V

Victor Bazarov

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...
 
Y

ypjofficial

Victor said:
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..
 
Y

ypjofficial

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
 
R

Richard Herring

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.
 
J

Jim Langston

Victor Bazarov wrote:

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...
 
Y

ypjofficial

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top