이진 비 호환성, 멤버에 액세스하거나 더 나쁜 클래스를 잘못된 클래스의 함수 호출
#pragma once
//include1.h:
#ifndef classw
#define classw
class class_w
{
public: int a, b;
};
#endif
함수가 그것을 사용하고 괜찮습니다.
//functions.cpp
#include <include1.h>
void smartFunction(class_w& x){x.b = 2;}
다른 버전의 수업 가져 오기 :
#pragma once
//include2.h:
#ifndef classw
#define classw
class class_w
{
public: int a;
};
#endif
main에서 함수를 사용하여 두 번째 정의는 클래스 정의를 변경합니다. 이진 비 호환성을 초래하고 런타임에 단순히 충돌합니다. main.cpp의 첫 번째 포함을 제거하여 문제를 해결하십시오.
//main.cpp
#include <include2.h> //<-- Remove this to fix the crash
#include <include1.h>
void smartFunction(class_w& x);
int main()
{
class_w w;
smartFunction(w);
return 0;
}
어떤 변형도 컴파일 또는 링크 시간 오류를 생성하지 않습니다.
그 반대의 경우, include를 추가하면 충돌이 수정됩니다.
//main.cpp
//#include <include1.h> //<-- Add this include to fix the crash
#include <include2.h>
...
이러한 상황은 이전 버전의 프로그램에서 버그를 수정하거나 외부 라이브러리 / dll / 공유 객체를 사용할 때 훨씬 더 어렵습니다. 그렇기 때문에 때때로 바이너리 역 호환성 규칙을 따라야합니다.