Topic: Problem with User-defined Conversions
Author: dwebster@cs.arizona.edu (Dave E. Webster, Jr.)
Date: 11 Jul 91 22:59:53 GMT Raw View
Can someone clarify the rules governing conversion functions? I have
written a String class (who hasn't?) which has as the following constructor:
String :: String (char s*);
and a member function strstr() with the following declaration:
size_t String :: strstr (String& pattern);
Everything works fine when I call strstr() with a String argument, but
when I call it with a char * argument, such as:
String s = "Test String";
size_t pos = s.strstr ("Str");
I get a nasty message from the compiler (Borland C++ 2.0) that reads:
'Type mismatch in parameter 'pattern' in call to 'String::strstr(String near&)'
I have examined the sections in the ARM concerning conversion functions
(12.3) and function argument matching (13.2) and I don't understand why
the compiler doesn't convert the char * argument to a temporary String
object, since a constructor is declared with exactly one argument of
type char *. Can someone clarify what is happening here? The only way
around this problem that I have come up with is to redefine the argument
to strstr() as char * and then define an explicit "operator char* ()" con-
version function to convert String parameters to char *. Although this
works, it defeats the purpose (and some of the functionality) of the class.
Thanks,
Dave.
Author: mat@mole-end.UUCP (Mark A Terribile)
Date: 13 Jul 91 22:20:29 GMT Raw View
In article <1644@caslon.cs.arizona.edu>, dwebster@cs.arizona.edu (Dave E. Webster, Jr.) writes:
>
> ... I have ... a String class ... which has ... :
> String :: String (char s*);
...
> size_t String :: strstr (String& pattern);
> when I call [strstr()] with a char * argument, such as:
> String s = "Test String";
> size_t pos = s.strstr ("Str");
> I get a nasty message from the compiler (Borland C++ 2.0) that reads:
> 'Type mismatch in parameter 'pattern' in call to 'String::strstr(String near&)'
> ... I don't understand why the compiler doesn't convert the char * argument
> to a temporary String object, since a constructor is declared with exactly
> one argument of type char *. Can someone clarify what is happening here?
You have two problems of the same sort. The double-quoted string
literal is treated as const char* , not char* . And your strstr()
needs a reference to String , so C++ assumes that it may modify the
argument and refuses (or will refuse, in ANSI C++) to insert a conversion
that requires a temporary. Your strstr() should accept an argument of
type const String& .
C++ requires attention to const . It's good practice anyway, but C++
gives it a payoff.
--
(This man's opinions are his own.)
From mole-end Mark Terribile
Author: pete@borland.com (Pete Becker)
Date: 15 Jul 91 16:53:10 GMT Raw View
In article <1644@caslon.cs.arizona.edu> dwebster@cs.arizona.edu (Dave E. Webster, Jr.) writes:
>Everything works fine when I call strstr() with a String argument, but
>when I call it with a char * argument, such as:
>
> String s = "Test String";
> size_t pos = s.strstr ("Str");
>
>I get a nasty message from the compiler (Borland C++ 2.0) that reads:
>
>'Type mismatch in parameter 'pattern' in call to 'String::strstr(String near&)'
>
It's a bug. The workaround is to explicitly cast the quoted string
to a String:
size_t pos = s.strstr( String("Str) );
-- Pete