j0mbolar wrote:
operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
}
generates the error:
"ANSI C++ forbids declaration `operator =' with no type
how should the return type be specified?
Asside from the return type, you should beware that your class
might self destruct in certain circumstances:
#include <stdlib.h>
#include <iostream>
class C {
public:
char * m_string;
C ( void ) :
m_string( 0 )
{}
const C& operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
return( *this );
}
};
int main ( void ) {
C c;
c = "Hello world!";
std::cout << c.m_string << std::endl;
c = c.m_string;
std::cout << c.m_string << std::endl;
}
This greets the world only once.
You might find std::string more usefull. BTW, I found that
std::string usually performs *better* than my onw hand coded classes
ontaining char*. The reason is, that with char* you have to do your
own memory management. The magic hidden inside the STL shipped with
the compiler usually outperforms my own code.
Best
Kai-Uwe