确定是否已覆盖某个函数 [英] Determine if a function has been overridden

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

问题描述

当我有一个指向该对象的指针作为它的基类(
$ b $)时,是否可以确定某个函数是否已被

对象覆盖b是抽象的)?我想要这样做的原因是我想要调用这个

函数,如果它被覆盖了,而不是如果它没有,例如<​​br />

class CBase

{

public:

virtual long mightBeOverridden(int i){return 0; } //不是纯粹的
虚函数,因为子类不应该定义它

bool isFunctionOverRidden(){return false; } //这就是我想要的代码

代码

virtual int id()= 0; //这个类是一个抽象类因为

这个函数 - 我不认为这是相关的,但更多的信息是好的

};


class CDerived1

{

//不覆盖函数

int id(){return 1; }

};


class Derived2

{

long mightBeOverridden(int i){return一世; }

//不应该也重写isAboveFunctionOverRidden()

int id(){return 2; }

};


class Derived3

{

long mightBeOverridden(int i){return我*我; }

//不应该也重写isAboveFunctionOverRidden()

int id(){return 3; }

};


int foo(CBase * pObj,int i,int j)

{

if(pObj)

{

if(pObj-> isFunctionOverridden())

return mightBeOverridden(i);

}


返回j;

}

我用Google搜索并且没有任何结果,搜索了这个列表。

我猜这意味着它是不可能的。我以为我可能会比较函数指针(返回mayBeOverridden!=

CBase :: mightBeOverridden)但是这两者并没有区别。


如果它是相关的,我正在使用MS Visual C ++ 7.0(又名.NET 2002),因为

很快升级到8.0。


在此先感谢,


斯图尔特

Is it possible to determine if a function has been overridden by an
object, when I have a pointer to that object as it''s base class (which
is abstract)? The reason I want to do this is that I want to call this
function if it has been overridden, and not if it hasn''t, e.g

class CBase
{
public:
virtual long mightBeOverridden(int i) { return 0; } // not a pure
virtual function, since sub-classes should not have to define it
bool isFunctionOverRidden() { return false; }// This is what I want
to code
virtual int id()=0; // this class is an abstract class because of
this function - I don''t think that this is relevant but more info is good
};

class CDerived1
{
// Does not override the function
int id() { return 1; }
};

class Derived2
{
long mightBeOverridden(int i) { return i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 2; }
};

class Derived3
{
long mightBeOverridden(int i) { return i * i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 3; }
};

int foo(CBase* pObj, int i, int j)
{
if(pObj)
{
if(pObj->isFunctionOverridden())
return mightBeOverridden(i);
}

return j;
}
I googled on this and came up with nought, as did a search of this list.
I''m guessing that means it''s impossible. I thought that I might
compare function pointers ( return mightBeOverridden !=
CBase::mightBeOverridden ) but that doesn''t distinguish between the two.

If it''s relevant, I''m using MS Visual C++ 7.0 (aka .NET 2002), due to
upgrade to 8.0 soon.

Thanks in advance,

Stewart

推荐答案

对不起,在我的例子中Derived类应声明如下。


CDerived1:public Base


CDerived2:public Base


CDerived3:公共基地


问题依然存在。
Sorry, in my example the Derived classes should be declared as follows.

CDerived1 : public Base

CDerived2 : public Base

CDerived3 : public Base

The problem remains.


S. I. Becker写道:
S. I. Becker wrote:
当我有一个指向该对象的指针作为它的基类时,是否有可能确定一个函数是否已被一个
对象覆盖(
是抽象的)?我想这样做的原因是我想要调用
这个函数如果它被覆盖了,而不是如果它没有,例如


这似乎是试图解决一个糟糕的设计。为什么在

世界中你会故意设计你的系统?

class CBase
{
公共:
虚拟长期mightBeOverridden( int i){return 0; } //不是纯粹的虚函数,因为子类不应该定义它
bool isFunctionOverRidden(){return false; } //这就是我想要的代码
virtual int id()= 0; //这个类是一个抽象类,因为这个函数 - 我不认为这是相关的,但更多的信息是好的};

类CDerived1
{
//不覆盖函数
int id(){return 1; }
};

类Derived2
{
long mightBeOverridden(int i){return i; //
//不应该重写isAboveFunctionOverRidden()
int id(){return 2; }
};

类Derived3
{
long mightBeOverridden(int i){return i * i; //
//不应该重写isAboveFunctionOverRidden()
int id(){return 3; }
};

int foo(CBase * pObj,int i,int j)
{
if(pObj)
{
if(pObj-> isFunctionOverridden())
return mightBeOverridden(i);


你的意思是


返回pObj-> mightBeOverridden(i);

}

返回j;
}

我用Google搜索了这个并且没有任何结果,就像搜索这个
列表一样。我猜这意味着它是不可能的。我以为我可能会比较函数指针(返回mayBeOverridden!=
CBase :: mightBeOverridden),但这并没有区分
两个。


我不认为没有一些技巧可以使用dynamic_cast

意味着''foo''必须知道派生类,反过来显示设计是坏的,这表明你需要重新访问

设计,而不是像那样修补它。


虚函数的全部意义在于它们应该是* b $ b使用*没有*任何关于派生类是否有或没有

重写了他们。如果你突然想要了解这一点,那么你可能首先要认为这些功能是虚拟的。

如果它是相关的,我我正在使用MS Visual C ++ 7.0(又名.NET 2002),因为很快就会升级到8.0。
Is it possible to determine if a function has been overridden by an
object, when I have a pointer to that object as it''s base class (which
is abstract)? The reason I want to do this is that I want to call
this function if it has been overridden, and not if it hasn''t, e.g
This seems like an attempt to work around a bad design. Why in the
world would you intentionally design your system like that?
class CBase
{
public:
virtual long mightBeOverridden(int i) { return 0; } // not a pure
virtual function, since sub-classes should not have to define it
bool isFunctionOverRidden() { return false; }// This is what I want
to code
virtual int id()=0; // this class is an abstract class because of
this function - I don''t think that this is relevant but more info is
good };

class CDerived1
{
// Does not override the function
int id() { return 1; }
};

class Derived2
{
long mightBeOverridden(int i) { return i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 2; }
};

class Derived3
{
long mightBeOverridden(int i) { return i * i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 3; }
};

int foo(CBase* pObj, int i, int j)
{
if(pObj)
{
if(pObj->isFunctionOverridden())
return mightBeOverridden(i);
You mean

return pObj->mightBeOverridden(i);
}

return j;
}
I googled on this and came up with nought, as did a search of this
list. I''m guessing that means it''s impossible. I thought that I
might compare function pointers ( return mightBeOverridden !=
CBase::mightBeOverridden ) but that doesn''t distinguish between the
two.
I don''t think it''s possible without some trick with dynamic_cast which
means that ''foo'' has to know about derived classes, which in turn shows
that the design is bad, which suggests that you need to revisit the
design instead of patching it up like that.

The whole point of virtual functions is that they are supposed to be
used *without* any regard to whether the derived class has or hasn''t
overridden them. If suddenly you have an urge to learn that, then
you probably shoudn''t have those functions virtual in the first place.
If it''s relevant, I''m using MS Visual C++ 7.0 (aka .NET 2002), due to
upgrade to 8.0 soon.




它不是。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问



It''s not.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


同意这将是一个糟糕的设计,但是如果你想这样做,

我没有尝试,只是简单地按理论


班级基础

{

public:

virtual void overrideFunc(){}

virtual int isFunctionOveride()

{

base refObj;

返回(& this-> overrideFunc!=& refObj.overrideFunc);

}

};


class derive1:公共基地

{

pub lic:

virtual void overrideFunc(){/ * overrided * /}

};

Agree this will be a bad design, but however if you want to do this,
I haven''t try but just simply by theory

class base
{
public:
virtual void overrideFunc() {}
virtual int isFunctionOveride()
{
base refObj;
return (&this->overrideFunc != &refObj.overrideFunc );
}
};

class derive1 : public base
{
public:
virtual void overrideFunc() { /* overrided */ }
};


这篇关于确定是否已覆盖某个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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