为什么我不能这样做? [英] Why can't I do this?

查看:85
本文介绍了为什么我不能这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的大师,


这是一个相当简单的问题,但我不知道答案是什么。

我可以轻易改变我的代码,但我想知道为什么我不能这样做

进一步了解C ++语言(和OO):


class a

{

public:

a(){vm(); }

virtual void vm()= 0;

};


class b:public a

{

public:

void vm(){}

};


int main()

{

b B;


返回0;

}


VC ++说:错误LNK2001:未解析的外部符号public:虚拟空白

__thiscall a :: vm(void)"

我明白''a''没有实现''vm'',但是我也不能创建''a''的实例(b)因为它有一个纯粹的虚拟

函数)所以当我创建一个''b''为什么不能'''的构造函数调用'''''''vm'' ?


Quentin。

解决方案

2003年9月17日星期三,格林尼治标准时间22:46:09 ;昆汀" < IH ******* @ SpamSux.com>写道:

这是一个相当简单的问题,但我无法弄清楚答案是什么。
我可以轻松更改我的代码,但我想知道为什么我不能这样做才能进一步了解C ++语言(和OO):

一级
公开:
a(){vm(); }
虚拟空虚vm()= 0;
};

b类:公共空间
{
公开:
void vm( ){}
};

int main()
{
b B;

返回0;
} < VC ++说:错误LNK2001:未解析的外部符号public:virtual void
__thiscall a :: vm(void)"

我理解' 'a''没有实现''vm'',但是我也无法创建''a'的实例(因为它有一个纯粹的虚拟
功能)所以当我创建一个''b'时,为什么''''''''''''''''''''''''''''''''''''''''''''这是常见问题解答。


在常见问题解答中查找。


如有必要,请先查询常见问题解答的位置(Google,这小组)。

Hth。,


- Alf


>在常见问题中查找。


谢谢Alf!


Quentin。


"昆汀" < IH ******* @ SpamSux.com>在消息中写道

新闻:Ru ********************* @ news1.calgary.shaw.ca ...

亲爱的大师,

这是一个相当简单的问题,但我无法弄清楚
的答案是什么。我可以轻松地更改我的代码,但我想知道为什么我不能这样做以进一步了解C ++语言(和OO):

class a {
public:
a(){vm(); }
虚拟空虚vm()= 0;
};

b类:公共空间
{
公开:
void vm( ){}
};

int main()
{
b B;

返回0;
} < VC ++说:错误LNK2001:未解析的外部符号public:virtual
void __thiscall a :: vm(void)"

我理解' 'a''没有实现''vm'',但是我也无法创建''a'的实例(因为它有一个纯粹的虚拟
功能)所以当我创建''b''为什么不能''构造函数调用''b''
''vm''?
Quentin。




因为在构建A部分this时指针仍然是A / B $ B类型,因此它对B还没有任何了解。换句话说,

构造从继承树的顶部开始,这个

指针的类型如下。 BTW,Mayers在他的书Effectife C ++中有一章关于

这个......你不应该(在大多数情况下)从

构造函数中调用虚函数。 />

马丁


Dear Gurus,

This is a fairly simple question, but I can''t figure out what the answer is.
I can easily change my code, but I want to know why I can''t do this to
further my knowledge of the C++ language (and OO):

class a
{
public:
a(){ vm(); }
virtual void vm() = 0;
};

class b : public a
{
public:
void vm() { }
};

int main()
{
b B;

return 0;
}

VC++ says: "error LNK2001: unresolved external symbol "public: virtual void
__thiscall a::vm(void)"

I understand that ''a'' doesn''t have a implementation of ''vm'', but also I
cannot create an instance of ''a'' either (because it has a pure virtual
function) so when I create a ''b'' why can''t ''a''s constructor call ''b''s ''vm''?

Quentin.

解决方案

On Wed, 17 Sep 2003 22:46:09 GMT, "Quentin" <IH*******@SpamSux.com> wrote:

This is a fairly simple question, but I can''t figure out what the answer is.
I can easily change my code, but I want to know why I can''t do this to
further my knowledge of the C++ language (and OO):

class a
{
public:
a(){ vm(); }
virtual void vm() = 0;
};

class b : public a
{
public:
void vm() { }
};

int main()
{
b B;

return 0;
}

VC++ says: "error LNK2001: unresolved external symbol "public: virtual void
__thiscall a::vm(void)"

I understand that ''a'' doesn''t have a implementation of ''vm'', but also I
cannot create an instance of ''a'' either (because it has a pure virtual
function) so when I create a ''b'' why can''t ''a''s constructor call ''b''s ''vm''?



This is a FAQ.

Look it up in the FAQ.

If necessary, first look up where the FAQ is (Google, this group).
Hth.,

- Alf


> Look it up in the FAQ.

Thanks Alf!

Quentin.


"Quentin" <IH*******@SpamSux.com> wrote in message
news:Ru*********************@news1.calgary.shaw.ca ...

Dear Gurus,

This is a fairly simple question, but I can''t figure out what the answer is. I can easily change my code, but I want to know why I can''t do this to
further my knowledge of the C++ language (and OO):

class a
{
public:
a(){ vm(); }
virtual void vm() = 0;
};

class b : public a
{
public:
void vm() { }
};

int main()
{
b B;

return 0;
}

VC++ says: "error LNK2001: unresolved external symbol "public: virtual void __thiscall a::vm(void)"

I understand that ''a'' doesn''t have a implementation of ''vm'', but also I
cannot create an instance of ''a'' either (because it has a pure virtual
function) so when I create a ''b'' why can''t ''a''s constructor call ''b''s ''vm''?
Quentin.



Because at the time of construction of A part "this" pointer is still of
type A, hence it does not know anything about B yet. In other words,
construction starts at the top of inheritance tree, and the type of this
pointer follows. BTW, Mayers in his book Effectife C++ has a chapter about
this... you shouldn''t (in most cases) call virtual functions from
constructors.

Martin


这篇关于为什么我不能这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆