First, I would recommend that you consider not providing an implicit conversion. You may find that the situations where unexpected conversions are not caught as errors outweighs the cost of calling encode
when you want a char*
.
If you do decide to provide an implicit conversion you declare it like this (inside your class definition.
operator char*();
You might be able to make the method const, in which case you can use:
operator char*() const;
Usually you would also want to return a pointer to a non-modifiable buffer:
operator const char*() const;
In the body of your function you should return
an appropriate pointer. As an implicit conversion clients wouldn't expect to have to free the returned buffer so if you need to make a special buffer for your return value you will have to maintain a pointer to this buffer until a suitable point to free it. Typically such a suitable moment might be the next mutating operation on your class object.
Note that as printf
takes any number and type of optional arguments you would still need to cast your class object in any case.
printf("%s\n", static_cast<const char*>(a + b + c[1] + d));
or
printf("%s\n", (const char*)(a + b + c[1] + d));
Both of these are more verbose than an explicit call to encode
.