Click here to Skip to main content
15,031,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my search for greater programming knowledge, I've ran into a little bump.
I've begun programming my own string class and operator overloading is where my problem is:

C++
class cString
{
private:
	// Variables
	char * string;
	int cStringLength;
	static const int STR_MAX = 25500;

	// Encapsulated Methods
	int GetStringLength(char*) const;
	void CopyToLocalString(char*);
	void DeleteLocalString();
	bool CompareString(char*) const;

public:
	// Constructors & Deconstructor
	cString();
	cString(char*);
	virtual ~cString();

	// Operator Overloads
	void operator = (cString&);
	void operator = (char*);
	char * operator = (cString&) const; // <- problem is here
	bool operator == (cString&) const;
	void operator + (cString&);

	// Accessor method
	char * GetString() const;
};


Well, all of the other operators work correctly, this the exception of the annotated function. At first the compiler was throwing a hissy fit about identical functions that only differ in return type, but that was resolved by making the other one a constant method.
The problem arises in actual execution as such
C++
int main()
{
        cString aString("sometext");
        char * strRef;

        strRef = aString;  // <- throws an error here

        return 0;
}

// Here is the function code
// cString string pointer to a char array pointer
char * cString::operator=(cString & cstring) const
{
	return this->string;
}


That's were I get the following error : cannot convert from 'cString' to 'char *'.
If I coded the assignment operator overload incorrectly, what is the correct syntax for such an operation?
If there is no direct way to assign a char pointer to a class member pointer, is there an indirect method?
Or, can this just not be done?
Posted

You can't overload the same operator by changing just the return type.
   
Are you trying to create a conversion operator?

For that the syntax would be
C++
operator char*() { return string; }
   
v2
Comments
Foothill 23-Sep-11 16:06pm
   
That worked. You learn something new about syntax every day.
Thank you.
André Kraak 23-Sep-11 16:11pm
   
Your welcome.
As John already explained, you cannot base method overload on return value.
That said, the operators
Foothill wrote:
C#
void operator = (cString&);
void operator = (char*);
char * operator = (cString&) const; // <- problem is here


are (at least) not recommended, since they break the language consistency: the assignment operator should return a reference to the object itself (that allows assignment chaining). As a general advice, operator overload is tricky and is usually worhty following well known guidelines (see for instance "C++ Operator Overloading Guidelines"[^], "Assignment operators" at C++ FAQ[^]).
   
Comments
Philippe Mori 23-Sep-11 18:35pm
   
Effectively, you should follows the guidelines in the above link. And readind Effective C++, More Effective C++ and Exceptionnal C++ among other would also be a good thing.
One suggestion: instead of overload assignments, overload constructors: constructors with a single argument are used for implicit conversion.

Assignments are automatically don by a call to an implicit conversion.
Overload them only if you have a more efficient way to handle the assignment respect to the copy.

The only assignment you are required to overload is the one with homogeneous parameter.

class mystring
{
public:
    mystring(); //default
    mystring(const char* literal); // C compatibility
    mystring( .... ); //whatever other conversion
    ~mystring(); //destroy: may by just clear();

    void clear(); //remove content and make it "as per default"
    void swap(mystring& s);  //exchange contents

    mystring(const mystring& src); // copy

    mystring& operator=(mystring src) //the only needed: and should be no more than this: note the copy in the paramenter
    { if(&src != this) { clear(); swap(src); } return *this; }

    /* C++0X only */
    mystring(mystring&& src); //move
    mystring& operator=(mystring&& src) //transfer
    { if(this!=&src) { clear(); swap(src); } return *this; }
};
   
HI,

As John and Pallini has explained, You can not overload the method depending on its return type.

I mean,
You can overload the method only if both the methods will have either different no of argument or different type of argument.
ex:
int add(int, int);
int add(int, float);
int add(float, int);
int add(float, float); //// these all can be successfully overload

but,
float add(int, int); // it will give you an error.

A small concept called "Name Mangling" is behind all these.
When you write any function definition, The c++ compiler internally changes it to new name
ex: add_ff (ff stands for float float) it means it take two args of type float

now you can see, the return type is not considered while mangling the function names. and hence function can not be overloaded depending on return type.

One more thing I would like mention is, name mangling is compiler dependent and hence the code compiled with Borland compiler won't run with vc++ compiler.

hope it will help you to understand this problem in detail.

thanks,
tushar
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900