Visual Studio Express 2013을 사용하고 C++에 익숙하지 않습니다. (Java에 대한 좋은 이해를 가지고 있지만이 문제는 저를 곤두박질칩니다.)문자열 변수가 클래스의 다른 인스턴스에서 공유 됨
"Dialogue"클래스의 "text"변수가 다른 인스턴스와 동일한 값을 공유하는 것 같습니다. 이 의미는 대화와 인쇄 모두의 결과입니다. " 콘솔에.
//Main Class
int main()
{
Dialogue dialogue1("Okay...");
Dialogue dialogue2("Then give it back!");
dialogue1.Print();
dialogue2.Print();
}
참고 :이 클래스를 사용하면 전역 변수와 같은 텍스트를 정의
//The "Dialogue Class"
#include "Dialogue.h"
#include <iostream>
#include <string>
using namespace std;
string text;
Dialogue::Dialogue(string dialogue)
{
text = dialogue;
}
Dialogue::~Dialogue()
{
}
void Dialogue::Print()
{
cout << text << endl;
}
코드가 전역 변수를 두 번 쓰고 있습니다 ... –
'main'은 클래스가 아니며 함수입니다. 자바와 달리 함수와 데이터는 C++ (네임 스페이스와 전역 네임 스페이스의 예제)에있는 클래스 외부에 존재할 수 있습니다. – JorenHeit