通过基类虚函数选择派生类文件全局变量
这是我面临的一个常见问题。 我打算为现有代码编写一个派生类。让我提供代码片段(仅作为示例):
Base_class_file:
const int addr=0xA;
Base_class::Read()
{
return *addr;
}
请注意,addr 变量是基类文件中的全局变量。 现在,我正在编写派生类,因为要更改一些功能并更改地址。我想重新使用 Read() 方法,并在派生类文件中提及新的 addr(假设为 0xB)。 由于此变量是全局变量,我如何向基类 Read() 方法提及使用 addr=0xB 而不是 0xA ?
非常感谢您的帮助。
This is a common problem I am facing.
I am intended to write a derived class for an existing code. Let me provide the code snippet (just example):
Base_class_file:
const int addr=0xA;
Base_class::Read()
{
return *addr;
}
Please note, the addr variable is a global variable in base class file.
Now, I am writing the derived class because to change some functionality and also to change the addr. I would like to re-use the Read() method, and just mention the new addr(let's say 0xB) in the derived class file.
Since this variable is a global variable, how do I mention to the base class Read() method to use the addr=0xB than the 0xA ??
Your help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样……
然后在子类中重写该方法以返回不同的结果?
How about....
and then overriding that method in the subclass to return a different result?
您在派生类中重写它并让它返回
0xB
。当然,只有当基类Read
方法被声明为virtual
时,这才有效。You override it in the derived class and have it return
0xB
. Of course this works only if the base classRead
method is declaredvirtual
.