Re: Conversion operator not being picked up by compiler
Re: Conversion operator not being picked up by compiler
- Subject: Re: Conversion operator not being picked up by compiler
- From: David Fang <email@hidden>
- Date: Fri, 13 Jan 2006 14:46:42 -0500 (EST)
Sorry to reply to myself...
Another suggestion embedded below:
> --- BEGIN ---
> #include <iostream>
>
> class Foo
> {
> public :
> explicit Foo( const char* message ) : m_str( message ) {}
>
> operator const char*() { return m_str.c_str(); }
> private :
> std::string m_str;
> };
>
> void Write( const char* msg, ... )
> {
> std::cout << msg << std::endl;
> }
// this will cause the compiler to look for a conversion to const char*
// when passing something else as the 2nd argument.
void Write(const char* msg, const char* str) {
// stuff goes here
}
> int main( int argc, char* const argv[] )
> {
> Foo foo( "Hello, World!" );
> Write( "%s", foo );
> return 0;
> }
> --- END ---
BTW, your code looks like it's going to print
%s
because none of the variadic arguments are actually used.
I'm sure you know what you meant.
David Fang
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden