由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - c++疑难问题。。
相关主题
求教:这个程序为什么不能编译?问个面试题
C++ Q83: 这个const_cast什么意思?这个C++程序的运行结果是什么
amazon的那道题目An example of strategy pattern
one C++ question请教C/C++小
C++: what is the output? How to interpret it?请教一个c的概念题
C++问题问个c++题
新手问个C++(Thinking in C++ source code)弱问个C++ 问题 (const_cast)
c++ 程序一问问个c++的问题
相关话题的讨论汇总
话题: const话题: int话题: std话题: public话题: vector
进入JobHunting版参与讨论
1 (共1页)
h**********y
发帖数: 1293
1
下面这段code为什么是输出1413。。最后一个为什么是3
#include
#include
class A
{
public:
A(int n = 0) : m_n(n) { }
public:
virtual int f() const { return m_n; }
virtual ~A() { }
protected:
int m_n;
};
class B
: public A
{
public:
B(int n = 0) : A(n) { }
public:
virtual int f() const { return m_n + 1; }
};
int main()
{
const A a(1);
const B b(3);
const A *x[2] = { &a, &b };
typedef std::vector V;
V y({ a, b });
V::const_iterator i = y.begin();
std::cout << x[0]->f() << x[1]->f()
<< i->f() << (i + 1)->f() << std::endl;

return 0;
}
q****m
发帖数: 177
2
polymophism 只能通过指针或者引用

【在 h**********y 的大作中提到】
: 下面这段code为什么是输出1413。。最后一个为什么是3
: #include
: #include
: class A
: {
: public:
: A(int n = 0) : m_n(n) { }
: public:
: virtual int f() const { return m_n; }
: virtual ~A() { }

h**********y
发帖数: 1293
3
iterator不也是包装好的指针么

【在 q****m 的大作中提到】
: polymophism 只能通过指针或者引用
d****n
发帖数: 12461
4
其实建vector用了copy ctor。

【在 h**********y 的大作中提到】
: iterator不也是包装好的指针么
p********4
发帖数: 58
5
我的理解,应该在vector中放地址,而不是object本身。这样才能实现多态。你原来的
做法,你的y中都是 class A. 不知道我的理解对不对。
int main()
{
const A a(1);
const B b(3);
const A *x[2] = { &a, &b };
typedef std::vector V;
V y({ &a, &b });
std::cout << x[0]->f() << x[1]->f() << std::endl;
for (auto i : y)
{
std::cout << i->f();
}
std::cout << std::endl;
return 0;
}
output:
14
14

【在 h**********y 的大作中提到】
: 下面这段code为什么是输出1413。。最后一个为什么是3
: #include
: #include
: class A
: {
: public:
: A(int n = 0) : m_n(n) { }
: public:
: virtual int f() const { return m_n; }
: virtual ~A() { }

w********s
发帖数: 1570
6
vector插入的时候把B copy了一下
copy以后,m_n是3,对象是A, 所以是3

【在 h**********y 的大作中提到】
: 下面这段code为什么是输出1413。。最后一个为什么是3
: #include
: #include
: class A
: {
: public:
: A(int n = 0) : m_n(n) { }
: public:
: virtual int f() const { return m_n; }
: virtual ~A() { }

w********s
发帖数: 1570
7
*i的类型是A,不是A*,所以调用的是A的f()

【在 h**********y 的大作中提到】
: 下面这段code为什么是输出1413。。最后一个为什么是3
: #include
: #include
: class A
: {
: public:
: A(int n = 0) : m_n(n) { }
: public:
: virtual int f() const { return m_n; }
: virtual ~A() { }

z*******5
发帖数: 30
8
this is an object slicing problem.
pzlife2014 的解释是对的。

【在 p********4 的大作中提到】
: 我的理解,应该在vector中放地址,而不是object本身。这样才能实现多态。你原来的
: 做法,你的y中都是 class A. 不知道我的理解对不对。
: int main()
: {
: const A a(1);
: const B b(3);
: const A *x[2] = { &a, &b };
: typedef std::vector V;
: V y({ &a, &b });
: std::cout << x[0]->f() << x[1]->f() << std::endl;

l**********g
发帖数: 503
9
+1

【在 w********s 的大作中提到】
: *i的类型是A,不是A*,所以调用的是A的f()
1 (共1页)
进入JobHunting版参与讨论
相关主题
问个c++的问题C++: what is the output? How to interpret it?
请教如何实现图的数据结构C++C++问题
问一个c++ virtual base class的问题新手问个C++(Thinking in C++ source code)
One C++ questionc++ 程序一问
求教:这个程序为什么不能编译?问个面试题
C++ Q83: 这个const_cast什么意思?这个C++程序的运行结果是什么
amazon的那道题目An example of strategy pattern
one C++ question请教C/C++小
相关话题的讨论汇总
话题: const话题: int话题: std话题: public话题: vector