[comp.lang.c++] denying assignment to a returned value

dsa@dlogics.UUCP (David Angulo) (10/27/89)

// Thanks, Andy and Doug, for pointing out potential coding errors in
//   libraries.  I did in fact fall into such a trap.  For example, I
//   coded:

class a
	{
	char[10] a_s;
    public:
	operator char *() {return a_s;}
	};

//
//   which is, of course, a mistake because a user of class a can code:

a an_a
strcpy((char*)an_a,"something");

//
// So, now we get to the question.  How can I do what Andy suggests with
//    "const String operator+(..."
//   I cannot code it like this:

class a
	{
	char[10] a_s;
    public:
	const operator char *() {return a_s;}
	};


//
//   because then I get the error message:

bad result type for a::operator char*()


//
// Neither can I code it like this:

class a
	{
	char[10] a_s;
    public:
	operator const char *() {return a_s;}
	};


//
//  because then I get the ubiquitous, stupid "syntax error."
// I can't even try to get tricky and try something like:

class a
	{
	char[10] a_s;
    public:
	const char* a_val() {return &a_s;}
	operator char *() {return a_val();}
	};

//
//   With this, I get:

bad return value type for a::operator char*(): const char* (char* expected)

//
//  So, the question is "How do I do it?"
-- 
David S. Angulo                  (312) 266-3134
Datalogics                       Internet: dsa@dlogics.UUCP
441 W. Huron                     UUCP: ..!uunet!dlogics!dsa
Chicago, Il. 60610               FAX: (312) 266-4473