こんにちは私はconst char *に奇妙な方法で同じ問題を抱えています。 Color :: Text()は静的char配列を返しますが、配列のconst char * pointerを初期化するためにも使用されます。Const char *奇妙な振る舞い
wtfもし私がrofl :: Defaultが実際に動作し、最新の "const char"にならないようにしたいのですか?Color :: Text?
#include <windows.h>
class Color
{
public:
int R;
int G;
int B;
int A;
Color(int r, int b, int g, int a = 255)
{
R = r;
G = g;
B = b;
A = a;
}
const char* Text()
{
static char Texts[124];
snprintf(Texts, 124, "%i %i %i %i", R, G, B, A);
return Texts;
}
}
class rofl
{
public:
const char* Default;
const char* Value;
rofl(const char* d)
{
Default = d;
Value = d;
}
}
rofl dood("0");
rofl doaaod(Color(0,0,0).Text());
rofl xxxx(Color(0,55,0).Text());
int main()
{
printf("%s %s\n", dood.Default, dood.Value);
printf("%s %s\n", doaaod.Default, doaaod.Value);
printf("%s %s\n", xxxx.Default, xxxx.Value);
return 0;
}
出力は次のようになります。
0 0
0 55 0 0 0 0
0 55 0 0 55 0
'rofl'のコンストラクタでは、' Default'と 'Value'が指すバッファ(例:演算子' new'を使用して)を作成します。次に、それらのバッファにデータをコピーします。さらに、 'char *'の代わりに 'std :: string'オブジェクトを使用すると、動的割り当てがきれいに管理されます。 – Peter