나 C++에 다음 코드 한 벡터에서 작동 :오해 가상 기능
#include <iostream>;
#include <vector>;
class A
{
public:
A(int n = 0) : m_n(n) { }
public:
virtual int value() const { return m_n; }
virtual ~A() { }
protected:
int m_n;
};
class B
: public A
{
public:
B(int n = 0) : A(n) { }
public:
virtual int value() const { return m_n + 1; }
};
int main()
{
const A a(1);
const B b(3);
const A *x[2] = { &a, &b };
typedef std::vector<A> V;
V y;
y.push_back(a);
y.push_back(b);
V::const_iterator i = y.begin();
std::cout << x[0]->value() << x[1]->value()
<< i->value() << (i + 1)->value() << std::endl;
system("PAUSE");
return 0;
}
컴파일러 반환 된 결과 : 1413
내가 올바른 결과는 1414이 될 것이라고 생각하기 때문에 나는 조금 혼란 스러워요 (함수 가상으로서). 이 프로그램의 행동을 어떻게 설명합니까?
'# include '지시문의 끝에';'를 쓰지 마십시오. – Fabien
@Fabien 나는 컴파일 된 프로그램에 놀랐다. – imulsion