default conversion operator

This is a discussion on default conversion operator within the C++ Programming forums, part of the General Programming Boards category; If I have function that accepts any type like the formatting function sprintf. I have a bunch of conversion operators ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    default conversion operator

    If I have function that accepts any type like the formatting function sprintf. I have a bunch of conversion operators for my IString class how do I make sure that when I call sprintf only the char* operator gets called? In essance how do I default the conversion to char*?

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    sprintf doesnt know how to handle the class IString, it would return an error.

    Just because it accepts any type as input doesn't mean it knows how to to handle every type. It knows how to convert base types only such as char*, double, int, etc.

    If you want to use you class IString with sprintf, then you need to do the conversion at the time you pass the IString to the function.

    For example:

    sprintf ( "hello, %s", myIStringVariable.c_str() );

    Assuming the member function c_str() returns the char* equivalent of an IString.

    Hope that helps!
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    Thanks for the response but it does not help me. I know what you just stated. What I am asking is:

    How do I default the conversion of an IString object to char*?

    I want to be able to call sprintf like this:

    sprintf(buffer, "hello %s and %s", str1, str2);

    where str1 and str2 are IString objects and they automatically get converted to char* through a default conversion operator like:

    IString :: operator char* cost
    {
    return buffer.data();
    }

    I have this operator defined but it does not automatically convert to char* in the above case.

    Am I making sense. I know that this "sprintf(buffer, "hello %s and %s", str1, str2);" is possible because we have done it before.

    Am I making sense?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Its not possible because those are unknown arguments. The function doesn't know what type they're supposed to be until it reads the format string (hence the different % escapes). C++ has to know to cast that class into the appropiate type. That means you need a (char *) before the variable name.

    The only way it could have worked is that you used a struct and the first data element was a fixed-length string/array. And that would be a design flaw anyways.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    Speedy5,

    Thanks for the response. I think you are wrong but I can't tell you why because if I knew I would have been able to solve my problem. I have seen the following call work before:

    IString str1("Mona"), str2("Lisa");
    sprintf(buffer, "hello %s and %s", str1, str2);

    here str1 and str2 automatically get converted to char*.

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    mangoMan: It is not possible... but....

    ...if you put the pointer to the C-string first in the class definition it might work. It's undefined according to the standard of course:
    Code:
    class IString
    {
    public: 
    	 //Methods
    private:
    	 char* internalString; //Needs to go first
    	 int someOtherVar;
    };
    You could try it out. There is no "safe" way to do this. Sorry.

    It would be much better if you forgot about the deprecated printf and wrote your own ostream& operator<<.

    EDIT: As I said in another thread:
    This is what happens when you call sprintf with the IString object:
    Code:
    void sprintf( char** pstr);
     
    //In main
    IString istr = "Petter";
    sprintf( static_cast<char**>(&istr) );
    The first four bytes (on Win32) of the IString object becomes a C-style string pointer.

    You see the problem? No high-level C++-conversion operators matter.
    Last edited by Sang-drax; 05-05-2004 at 10:47 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  3. int conversion operator
    By colorado_scott in forum C Programming
    Replies: 2
    Last Post: 04-04-2003, 01:17 PM
  4. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM
  5. Overriding = operator.
    By Strahan in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2001, 03:26 PM

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