Thread: implicit conversion not working

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    implicit conversion not working

    In my string class I have a constructor for type conversions from char * to string (the name of my string class). My program doesn't implicitly convert char * to string when I say something like
    Code:
    string test;
    test = "foo";
    I get an illegal operand error if I try something like that.
    My constructor works fine if I say
    Code:
    string test = "foo";
    Does anybody know why this is?
    Here's my code, it will probably help:
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    	class string
    	{
    	protected:
    		char * str;
    		unsigned int len;
    		unsigned int strlen(char *);
    	public:
    		string() : len(0), str(0) {}
    		string(char *);
    		string & operator=(string &);
    	};
    	unsigned int string::strlen(char * s)
    	{
    		unsigned int a = 0;
    		while (s[a] != '\0')
    			a++;
    		return a;
    	}
    	string::string(char * s)
    	{
    		unsigned int slen;
    		len = slen = strlen(s);
    		str = new char[slen];
    		for(unsigned int a = 0; a < slen; a++)
    			str[a] = s[a];
    	}
    	string & string::operator=(string & s)
    	{
    		delete [] this->str;
    		this->str = new char[s.len];
    		this->len = s.len;
    		for(unsigned int a = 0; a < s.len; a++)
    			this->str[a] = s.str[a];
    	}
    
    int main()
    {
    	string test;
    	test = "beef lo mein";
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    It's fairly simple. When you try this line:
    string str = "Testing";

    You are invoking the function
    string::string(char *);
    implicitly.

    When you try:
    string str;
    str = "Testing";
    You are invoking the default constructor, followed by the operator=(char *), which you have not provided.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    My book says that my function to convert char * to string will be called when I assign a char * value to a string object, when I pass a char * value to a function expecting a string argument when a function that's declared to return a strng value tries to return a char * value and when I initialize a string object to a char * value. How can I do these things?

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    Your book is incorrect. Constructors are only invoked when you are creating a new object. (The other cases you mentioned will use your type conversion constructor) In the case of assignment, you are modifying an existing object. You'll have to overload the assignment operator as IfYouSaySo stated.
    Code:
    string& operator=(char* s) 
    {
      delete[] str;
      len = strlen(s);
      str = new char[len];
      strcpy(str, s);
      return *this;
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    My book says that my function to convert char * to string will be called when I assign a char * value to a string object, when I pass a char * value to a function expecting a string argument when a function that's declared to return a strng value tries to return a char * value and when I initialize a string object to a char * value.
    The second case is
    void func(string s);
    which you call with
    func("bla");

    The third case
    Code:
    string func() {
      return "bla";
    }
    The fourth case is what you've shown as working.

    But in the first case I believe your book is incorrect. It requires first the construction of a temporary string object using the conversion constructor, then the assignment of this to the string object. Maybe the compiler should do it, but I doubt any do.
    You can either supply a char * assignment operator or explicitly cast to string. The former is more efficient and intuitive.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Currency Conversion
    By roadrunnr70 in forum C Programming
    Replies: 8
    Last Post: 12-16-2004, 03:47 PM
  4. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM
  5. Help with temperature conversion Chart
    By sanmaximo in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2003, 03:29 PM