#include <iostream>
using std::cout;
using std::endl;
class aClass
{
public:
aClass()
{
this->aString = new char [1];;
this->aString[0] = '\0';
this->length = 0;
}
~aClass()
{
cout << "Destructor" << endl;
delete [] this->aString;
}
aClass(const char *);
const aClass& aClass::operator=(aClass &);
private:
int length;
char * aString;
};
aClass::aClass(const char * text): length (strlen(text))
{
cout << "Conversion constructor " << endl;
cout << "\ttext is '" << text << "'" << endl;
this->aString = new char [this->length + 1];
strcpy(this->aString, text);
}
const aClass & aClass::operator=(aClass & right)
{
cout << "In Overloaded = operator" << endl;
if (&right != this)
{
delete [] this->aString;
this->aString = new char [right.length + 1];
strcpy(this->aString, right.aString);
}
return *this;
}
int main()
{
aClass aObj("string");
aObj = "string2";
return 0;
}
From what I read, what should happen with what I've written is the string "string2" should be converted to a temporary "aClass" Object using the conversion constructor.
This aClass object should then be passed to the overloaded operator= in aObj to do the assignment.
Instead I'm getting the following error
file.cpp: In function `int main()':
file.cpp:53: no match for `aClass& = const char[8]' operator
file.cpp:36: candidates are: const aClass& aClass::operator=(aClass&)
What have I missed?