Path: utzoo!attcan!uunet!wuarchive!udel!rochester!newkodak!kodak!ektools!randolph From: randolph@ektools.UUCP (Gary L. Randolph) Newsgroups: comp.lang.c++ Subject: Re: implicit conv to int in subscript Message-ID: <2670@ektools.UUCP> Date: 30 May 90 14:22:36 GMT References: <684@dyndata.UUCP> Sender: randolph@ektools (Gary L. Randolph) Reply-To: randolph@ektools.UUCP (Gary L. Randolph) Organization: Eastman Kodak, Dept. 47, Rochester NY Lines: 57 In article <684@dyndata.UUCP> dan@dyndata.UUCP (Dan Everhart) writes: # #Consider the following program: # #class Index # { #public: # Index (int i) : v (i) {}; # operator int () { return v; }; #private: # int v; # }; # #Index x (0); #char ac [10]; # #f () # { # ac [x] = 'a'; // this line fails ^^^^^^^^^^^^^^^It shouldn't # # ac [(int) x] = 'a'; # ac [int (x)] = 'a'; # } # # #On the marked line, I expected the compiler to make an implicit #conversion of x to int, since the Index class has an operator int () #function defined. Zortech 2.06 gives the error message: Your expectation is valid! #"opint.cpp", line 17 Syntax error: illegal pointer arithmetic #Had: #and: #The subsequent two lines, with their explicit conversions, compile #fine. You should not have to cast! #Although I could not find any example in The C++ Primer of exactly #this nature, there is a close one on p. 283 where Lippman defines a #SmallInt class, with similar conversion operators and says, "A #SmallInt class object can now be used *anywhere* in int object can be #used." (Emphasis mine.) Lippman is correct. #So, should this work? Yes. I have tested your code using Sun C++ 2.0 and all is as you had hoped! (minor point: Obviously f() should be defined as a void function) Gary