함수 헤더가 있습니다.
double countThis(double counter);
그런 다음 내 메인에서 다음을 수행합니다.
double test = 10;
countThis(test);
그런 다음 기능이 제공됩니다.
double countThis(double counter) {
double counted = counter;
return counted;
}
맨 아래에는 마지막 함수가 하나 있습니다. 여기서는 double counted
하지 않고 얻고 countThis(something)
싶습니다. main에서 이전 호출에서 반환 된 값을 얻고 값 10 (계산 됨)을 얻고 싶습니다.
이러한 종류의 지속성을 달성하는 한 가지 방법은 클래스를 사용하고 해당 클래스 의 인스턴스 를 정의하는 것 입니다.
struct Counter
{
double counted;
double countThis(double counter)
{
return counted = counter; // assign counter to counted, and return that value.
}
};
사용 시점 :
int main()
{
Counter c;
c.countThis(10);
// c.counted contains the last value sent to countThis, in this case, 10
}
인스턴스가 c
사용됩니다 계속 당신이 통과하는 값을 countThis
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다