CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Posts
    4

    operator char*() not working?

    Hi All,
    I'm playing with a custom string class.

    //////////////////////////////////// in mystring.h
    class mystring {
    char* p;
    public:
    mystring(): p(0) {}
    mystring(char*);
    ~mystring();
    operator char*() { return p; } // operator char*() defined here

    friend ostream& operator<< (ostream&, const mystring&);
    };

    //////////////////////////////////// in mystring.cpp
    ostream& operator<< (ostream& os, const mystring& str)
    {
    os << (char*)str; // ERROR REPORTED HERE – compiler can't find operator char*()
    return os;
    }

    //////////////////////////////////// in main.cpp
    void main()
    {
    mystring hello = "Hello World!";
    cout <<hello;
    }

    I get the following:
    error C2440: 'type cast' : cannot convert from 'const class mystring' to 'char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    WHAT? Operator char*() is defined right there! What am I doing wrong?

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Note the const in your compiler's error message and modify your operator so that it is a const function.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Feb 2004
    Posts
    21
    First: you have to reserve memory for p:

    I hope you do that in mystring(char */*put variable- it's more clear*/);

    And do that mystring(): p("") {}

    Second: i think that code does not make sense. I mean it would be usefull if you convert this char * to integer, but I don't know, as you wish. And I'm not sure whether you can overload char*(at least I haven't seen it before) but I will search about this.

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Code:
    class mystring 
    {
    	char* p;
    
    public:
    	// You two constructors can be combined into one by
    	// using default value.
    	mystring(char* s=0): p(s) {}
    	~mystring();
    
    	// There is no need for this typecast operator in your
    	// example. However, if you want to use it in operator<<,
    	// you need to declare it as a constant function.
    	operator char*() const{ return p; } 
    
    	friend ostream& operator<< (ostream&, const mystring&);
    };
    
    ostream& operator<< (ostream& os, const mystring& str)
    {
    	// Since this is a friend function, you can use the member
    	// variable of mystring directly. There isn't any need
    	// to declare and implement a "operator char*()".
    	os << str.p; 
    	return os;
    
    	// Alternative, if you like to do typecasting.
    	//os << static_cast<char*>(str);
    	//return os;
    }

  5. #5
    Join Date
    Feb 2004
    Posts
    21
    Well, yes you can and what Kheun has done is working. Look it!
    Last edited by defiler_z; March 4th, 2004 at 08:30 PM.

  6. #6
    Join Date
    Mar 2004
    Posts
    4
    operator char*() const

    That was it! Thanks very much all!

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Of course, that could lead to all sorts of problems... the best thing to do with user-defined conversions is to forget them. Do it explicitly, with a named function and you won't get hard-to-find bugs as the compiler puts in an implicit conversion that you don't notice and don't want.

    If you still want to do implicit conversions, I would suggest that you provide these two:
    Code:
    class mystring
    {
    public:
        operator const char * () const;
        operator char * ();   // optional (and not recommended)
    };
    
    ostream& operator<<(ostream& os, const mystring& s)
    {
        return os << static_cast<const char *>(s);
    }
    However, a better solution would be:
    Code:
    class mystring
    {
    public:
        const char * c_str() const;
        char* c_str();   // optional (and not recommended)
    };
    
    ostream& operator<<(ostream& os, const mystring& s)
    {
        return os << s.c_str();
    }
    I've marked the two non-const functions as "not recommended" because they expose the internals of your class and, again, can lead to hard-to-find bugs when something modifies your data member without you knowing about it.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Mar 2004
    Posts
    4
    Thanks Graham! I found this to work (it returns a const char* so anyone using the class cannot modify *p)

    operator const char*() const

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured