아래 프로그램에서 check라는 부울 변수를 사용합니다.이 함수는 Tst1과 Test2의 두 개체로 주 함수 내에서 액세스됩니다. 그러나 프로그램에서 check 변수의 값은 유지되지 않습니다. 우리는 정적을 사용할 수 있지만 다른 대안을 알고 싶습니다. 누군가 나에게 힌트를 줄 수 있습니까? 미리 감사드립니다. 정적 키워드를 사용하지 않고 전체 프로그램에서 데이터 멤버의 일관성을 유지
내부 jointdeatils.h
#pragma once
class Jointdetails
{
public:
Jointdetails(void);
~Jointdetails(void);
bool check;
};
내부 jointdeatils.cpp analyzer.cpp 내부 analyzer.h
#pragma once
#include "Jointdetails.h"
class Analyzer
{
public:
Analyzer(void);
Jointdetails* GetJointDetails();
Jointdetails* m_ptheCTJointDetails;
~Analyzer(void);
};
내부
#include "Jointdetails.h"
Jointdetails::Jointdetails(void)
{
check = false ;
}
Jointdetails::~Jointdetails(void)
{
}
#include "Analyzer.h"
#include "stddef.h"
Analyzer::Analyzer(void)
{
m_ptheCTJointDetails = new Jointdetails();
}
Analyzer::~Analyzer(void)
{
}
Jointdetails* Analyzer::GetJointDetails()
{
if(m_ptheCTJointDetails)
return m_ptheCTJointDetails;
else
return NULL;
}
,691 엄마 내부 363,210
내부 test1.h를
#pragma once
#include "Analyzer.h"
class Tst1
{
public:
Tst1(void);
Analyzer *analyzer1 ;
public:
~Tst1(void);
};
내부 Test1.cpp
#include "Tst1.h"
Tst1::Tst1(void)
{
analyzer1 = new Analyzer ;
}
Tst1::~Tst1(void)
{
}
내부 Test2.h
#pragma once
#include "Analyzer.h"
class Test2
{
public:
Test2(void);
Analyzer *analyzer2 ;
public:
~Test2(void);
};
내부 Test2.cpp
#include "Test2.h"
Test2::Test2(void)
{
analyzer2 = new Analyzer ;
}
Test2::~Test2(void)
{
}
in.cpp
#include "Test2.h"
#include "Tst1.h"
#include "stdio.h"
int main()
{
Tst1 *test1 = new Tst1 ; //check = false
Test2 *test2 = new Test2 ; //check = false
test1->analyzer1->GetJointDetails()->check = true ;
if(test2->analyzer2->GetJointDetails()->check)
printf("Check value is changed");
else
printf("Check value is not changed");
return 0 ;
}
그래서
JointDetails.h
JointDetails.cpp을 ... 당신은 디자인 냄새를 찾고 있어요? – xtofl
'check'는 인스턴스 변수이므로 각 객체는 자신 만의 버전입니다. 왜'정적'을 사용하고 싶지 않은가? –
@SethCarnegie 정적은 프로그램이 끝날 때까지 메모리를 사용하기 때문입니다. 그러나 나는 그것을 사용한 후에 기억을 풀고 싶다. – Kenta