虚函数问题 [英] Virtual function question

查看:110
本文介绍了虚函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我有一个定义了虚函数的基类:

class CBase {

virtual void Foo();

};


无效CBase :: Foo()

{

//基类功能

}


和一个覆盖基类Foo()的派生类:

class CDerived: public CBase {

void Foo();

};


CDerived :: Foo()

{

//派生功能

}


如果我有一个指向CDerived类型对象的指针,怎么能我打电话给基地

类版本的Foo()而不是多态版本?

CDerived派生;

CDerived * p_derived =& derived ;

p_derived-> Foo(); //想要以某种方式调用基类Foo()!


我认为有一种简单的方法可以做到这一点,但我找不到它。


谢谢,

B

解决方案

BCC发布:



如果我有一个定义了虚函数的基类:
类CBase {
virtual void Foo();
} ;

void CBase :: Foo()
//
//基类功能

和派生类覆盖了基类Foo():
类CDerived:public CBase {
void Foo();
};

CDerived :: Foo()
{
//派生的功能


如果我有一个指向CDerived类型对象的指针,我该如何调用Foo()的基类版本比多态版本?
CDerived派生;
CDerived * p_derived =& derived;
p_derived-> Foo(); //想以某种方式调用基类Foo()!



p_derived-> CBase :: Foo();

-JKop




" BCC" <峰; br *** @ akanta.com>在留言中写道

news:kQ ******************* @ newssvr29.news.prodigy。 com ...



如果我有一个定义了虚函数的基类:
类CBase {
virtual void Foo() ;
};

void CBase :: Foo()
//
//基类功能
}

一个派生类,它覆盖基类Foo():
class CDerived:public CBase {
void Foo();
};

CDerived :: Foo( )
//
//派生的功能

如果我有一个指向CDerived类型对象的指针,我该如何调用基类
类版本的Foo()而不是多态版本?
CDerived派生;
CDerived * p_derived =& derived;
p_derived-> Foo(); //想以某种方式调用基类Foo()!

我认为有一种简单的方法可以做到这一点,但我找不到它。
谢谢,
B




你确定要做那样的事吗?你有一个

派生类的对象,它定义了自己的行为,你想强迫它使用它的父亲的行为吗?我想你可能有设计问题。你试图告诉对象'自己的行为是不正确的。


DrX


< blockquote>

" BCC" <峰; br *** @ akanta.com>在留言中写道

news:kQ ******************* @ newssvr29.news.prodigy。 com ...



如果我有一个定义了虚函数的基类:
类CBase {
virtual void Foo() ;
};

void CBase :: Foo()
//
//基类功能
}

一个派生类,它覆盖基类Foo():
class CDerived:public CBase {
void Foo();
};

CDerived :: Foo( )
//
//派生的功能

如果我有一个指向CDerived类型对象的指针,我该如何调用基类
类版本的Foo()而不是多态版本?
CDerived派生;
CDerived * p_derived =& derived;
p_derived-> Foo(); //想以某种方式调用基类Foo()!

我认为有一种简单的方法可以做到这一点,但我找不到它。
谢谢,
B




你能做什么(以及有意义的)如下:


class CBase {

public:


virtual void Foo(){

... //基类行为
}

};


class CDerived:public CBase {

public:

void Foo(){

:: Foo(); //调用基类版本

... //扩展基类行为

}

};


Hi,

If I have a base class with a virtual function defined:
class CBase {
virtual void Foo();
};

void CBase::Foo()
{
// base class functionality
}

and a derived class which overrides the base class Foo():
class CDerived: public CBase {
void Foo();
};

CDerived::Foo()
{
// derived functionality
}

If I had a pointer to an object of type CDerived, how can I call the base
classes version of Foo() rather than the polymorphic version?
CDerived derived;
CDerived* p_derived = &derived;
p_derived->Foo(); // Want to somehow be able to call base class Foo()!

I think there is a simple way to do this, but I can''t find it.

Thanks,
B

解决方案

BCC posted:

Hi,

If I have a base class with a virtual function defined:
class CBase {
virtual void Foo();
};

void CBase::Foo()
{
// base class functionality
}

and a derived class which overrides the base class Foo():
class CDerived: public CBase {
void Foo();
};

CDerived::Foo()
{
// derived functionality
}

If I had a pointer to an object of type CDerived, how can I call the base
classes version of Foo() rather than the polymorphic version?
CDerived derived;
CDerived* p_derived = &derived;
p_derived->Foo(); // Want to somehow be able to call base class Foo()!


p_derived->CBase::Foo();
-JKop



"BCC" <br***@akanta.com> wrote in message
news:kQ*******************@newssvr29.news.prodigy. com...

Hi,

If I have a base class with a virtual function defined:
class CBase {
virtual void Foo();
};

void CBase::Foo()
{
// base class functionality
}

and a derived class which overrides the base class Foo():
class CDerived: public CBase {
void Foo();
};

CDerived::Foo()
{
// derived functionality
}

If I had a pointer to an object of type CDerived, how can I call the base
classes version of Foo() rather than the polymorphic version?
CDerived derived;
CDerived* p_derived = &derived;
p_derived->Foo(); // Want to somehow be able to call base class Foo()!

I think there is a simple way to do this, but I can''t find it.

Thanks,
B



Are you sure you want to do something like that? You have an object of a
derived class that defines its own behavior and you want to force it to use
its parent''s behavior? I think you may have a design problem. You are
trying to tell an object that''s own behavior is incorrect.

DrX



"BCC" <br***@akanta.com> wrote in message
news:kQ*******************@newssvr29.news.prodigy. com...

Hi,

If I have a base class with a virtual function defined:
class CBase {
virtual void Foo();
};

void CBase::Foo()
{
// base class functionality
}

and a derived class which overrides the base class Foo():
class CDerived: public CBase {
void Foo();
};

CDerived::Foo()
{
// derived functionality
}

If I had a pointer to an object of type CDerived, how can I call the base
classes version of Foo() rather than the polymorphic version?
CDerived derived;
CDerived* p_derived = &derived;
p_derived->Foo(); // Want to somehow be able to call base class Foo()!

I think there is a simple way to do this, but I can''t find it.

Thanks,
B



What you can do (and what makes sense) is the following:

class CBase {
public:

virtual void Foo() {
... //base class behavior
}
};

class CDerived : public CBase {
public:
void Foo() {
::Foo(); // call base class version
... // extend base class behavior
}
};


这篇关于虚函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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