About conversion operator

This is a discussion on About conversion operator within the C++ Programming forums, part of the General Programming Boards category; What does a conversion operator excatly does, more precisely what does it return? Before going any further read this Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    129

    About conversion operator

    What does a conversion operator excatly does, more precisely what does it return? Before going any further read this

    Code:
    #include <stdio.h>
    
    #define BUG
    
    class Foo
    {
      private:
    	short int bar;
    #ifdef BUG
      char c;
    #endif
      public:
    	Foo() :
    		bar(100)
    #ifdef BUG
    		,c(12)
    #endif
    		{}
    	operator short int () { return bar; }
    };
    
    void foobar(short int n)
    {
    	printf("%d\n", n);
    }
    
    int main()
    {
    	Foo foo;
    	printf("%d\n", foo);
    	foobar(foo);
    	return 0;
    }
    output while compiling:
    G:\source\test>gxx foobar.cpp -Wall
    foobar.cpp: In function `int main()':
    foobar.cpp:32: warning: int format, Foo arg (arg 2)

    output while BUG is not defined:
    100
    100

    and when BUG is defined
    786532
    100

    So, all works fine if I don't declare `c' but that's not the case. It's also quite surprising how `foobar()' works just fine in both times and direct `printf()' call doesn't. So it must be because stdarg.h that printf is using in it's declaration, right?

    Anyway, is there any way to retrieve the value of `bar' so that a) it works if `c' is declared and most important b) it doesn't give me a warning ie. to do it as supposed? All this without ``short int GetBar() const { return bar; }'' of course.
    Last edited by kooma; 12-31-2001 at 12:24 PM.
    kooma - t.h@iki.fi

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Ah, I see that the parsing system has changed... Luckily, it's not anything bigger... Sorry about that

    There was a FAQ here somewhere...

    ..edited..
    Doesn't look much better now...
    Last edited by kooma; 12-31-2001 at 12:25 PM.
    kooma - t.h@iki.fi

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You'll either have to use iostream for output or an explicit cast. printf() doesn't do any type checking so won't call your conversion constructor.
    zen

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    So, going under the hood, what does
    operator short int () { return bar; }
    gives me (and what type)?
    kooma - t.h@iki.fi

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    " what does
    operator short int () { return bar; }
    gives me (and what type)?"

    It returns bar (no cast neccesary) by value as a short int, whenever a short int is acceptable (IE, function accepting int/short int, or less acceptably taking a char) but a Foo is passed.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21