Frage

Ich habe den folgenden Code, der eine Unicode-String-Klasse aus einer Bibliothek verwendet, dass ich schreibe:

#include <cstdio>
#include "ucpp"
main() {
  ustring a = "test";
  ustring b = "ing";
  ustring c = "- -";
  ustring d;
  d = "cafe\xcc\x81";
  printf("%s\n", (a + b + c[1] + d).encode());
}

Das Kodierungsverfahren der ustring Klasseninstanzen wandelt das interne Unicode in einen UTF-8 char *. Da ich jedoch keinen Zugriff auf die Definition char Klasse, ich bin nicht sicher, wie ich eine implizite Typumwandlung definieren kann (so, dass ich nicht manuell Anruf kodieren muß, wenn sie mit printf, etc).

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top