Casting user defined objects.

This is a discussion on Casting user defined objects. within the C++ Programming forums, part of the General Programming Boards category; If we have a class like this: Code: class myClass{ public: myClass(); ~myClass(); private: char *myString; }; Can define a ...

  1. #1
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Casting user defined objects.

    If we have a class like this:
    Code:
    class myClass{
    public:
         myClass();
         ~myClass();
    private:
         char *myString;
    };
    Can define a function that can cast an object of the class myClass to a char *, so that if we canm write the following:
    Code:
    myClass object1;
    
    cout << object1;     //so this makes the  casting.
    If I can do this, can you tell me how?

    Thanks is advance.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    That's not really casting. You're just wanting to be able to cout your object's string with just the object - correct?


    Code:
    class myClass{
    public:
     myClass()
     : public_str(myString){ /*   */ }
     ~myClass();
    const char *public_str;
    friend ostream& operator << ( ostream& stream, myClass& object ){
     stream << object.public_str;
     return stream;
     }
    private:
     char *myString;
    };
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi, thanks for the reply, but what I meant was not overloading the << operator.
    I wanted to now if there is another way to do cout << object;
    for example like casting, because I read that casting can do the same job of overloading if we want to output the strings in an object.
    Does anybody have any idea about this?
    Can we overload the casting operator?

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Yeh you can cast the object to an array of characters if you like.

    You could do something like:

    char* bytes = (char*)object1;

    In this case you should also use sizeof(object) to get the number of bytes of your object, because your char* will not be null terminated and cannot be treated as a string.

    HOWEVER, are you sure you want to do this? If there are pointers in you object, you are only going to stream the value of the pointers, and not the content of what they point to. If you want to store values of data members in your object, then there will be better ways to do this.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    I think you are hinting at an implicit conversion

    Code:
    #include <iostream>
    
    class myClass{
    public:
         myClass(const char *String):myString(String){}
         operator const char*(){return myString;}
    private:
         const char *myString;
    };
    
    
    
    int main(void)
    {
    	myClass object1("Hello World");
    
    	std::cout << object1;     	
    
    }
    Its not preferable to Seb's method mind you.....implicit conversions can cause lots of ambiugity problems and unwanted conversions.......that's why std::string insists on you using the c_str() member.....those guys knew the problems of implicit conversion, so they avoided overloading operator const char*()

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    I have never encountered any serious problems with implicit casting (aside from some template confusion). I think it's superior to overloading cout, personally. Which problems are you referring to Fordy?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Sebastiani
    I have never encountered any serious problems with implicit casting (aside from some template confusion). I think it's superior to overloading cout, personally. Which problems are you referring to Fordy?
    Its because when you decide to implicitly cast, you are giving the compiler the opportunity to cast wherever it can even if the situation is not what you want......it can lead to small errors becoming big ones.....

    Say for instance, you want to add to my above class and add operator+() to concatanate string.....all very well..

    std::cout << obj1 + obj2;//great

    ..but what if you make a small mistake and do this

    std::cout << obj1 - obj2;//woops '-' not '+'

    Now it should not compile.....but it does, because subtracting 2 pointers is ok.......even if for your object is gives completely the wrong result. So by allowing the conversion, you must take all responsibility for the possible conversions the compiler may see fit to make - not very realistic......so that's why it is advised to avoid implicit conversion and that's why in libraries like the std library, its avoided where it might be easilly abused (as in std::string)

    It's discussed in More Effective C++ by Scott Meyers and in Exceptional C++ by Herb Sutter (the latter is where I pinched the above example BTW)

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Thank you all, you made the idea clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user defined header files
    By sidu in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2008, 06:40 AM
  2. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Stl lists and user defined types
    By figa in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2005, 11:09 AM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM

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