Запутанное поведение строки C ++ возвращает и C_STR () Casts [Duplicate]C++

Программы на C++. Форум разработчиков
Anonymous
Запутанное поведение строки C ++ возвращает и C_STR () Casts [Duplicate]

Сообщение Anonymous »

Я новичок в программировании C ++ (в основном работаю с Java), и такое поведение классов C ++, строк членов и преобразования строк в const char* с c_str () сбивает с толку.

sample.h < /p>

Код: Выделить всё

class Sample
{
private:
int id;
std::string text;
public:
Sample(int id);
void setId(int id);
int getId();
void setText(std::string txt);
std::string getText();
void loadText();
~Sample();
}
< /code>

 sample.cpp[/b]

Sample::Sample(int id)
{
this->id = id;
}

void Sample::setId(int id)
{
this->id = id;
}

int Sample::getId()
{
return this->id;
}

void Sample::setText(std::string txt)
{
this->text = txt;
}

std::string Sample::getText()
{
return this->text;
}

void Sample::loadText()
{
this->text = "Loaded";
}

Sample::~Sample()
{
std::cout loadText();
const char* text = sample->getText().c_str();
std::cout getText();
const char* text = textCpy.c_str();
std::cout 

Подробнее здесь: [url]https://stackoverflow.com/questions/60555228/confusing-behavior-of-c-string-returns-and-c-str-casts[/url]

Вернуться в «C++»