Zhangquan1987
BAN USERThis is a typical problem when pointer of the based class points to an object of the derived class. In this kind of problem, if the called function has been overridden by the derived function, the pointer will call its derived function. Thus the output is: "sum of class b"
- Zhangquan1987 May 17, 2013#include <iostream>
#include <vector>
using namespace std;
void test1(vector<int> arr);
void test2(const vector<int> &arr);
int main()
{
vector<int> arr;
for(int i=0;i<10;i++) {
arr.push_back(i);
}
test1(arr);
test2(arr);
return 0;
}
void test1(vector<int> arr) {
for(int i=0;i<arr.size();i++) {
cout<<arr.at(i)<<endl;
}
}
void test2(const vector<int> &arr) {
for(int i=0;i<arr.size();i++) {
cout<<arr.at(i)<<endl;
}
}
// When testing it, there is no difference in output.
- Zhangquan1987 May 17, 2013
It is a java question rather than C++ question.
- Zhangquan1987 May 17, 2013