编辑 2:
这里是我想做的事情的简单总结(我认为): 我想根据运行时计算的条件动态创建全局实例。
如果您想查看示例代码,可以跳到 EDIT1,但此时,上面的粗体文本可能是最容易理解的...
结束编辑 2.
我的问题是关于多态和继承。具体来说,我想知道是否有一种方法可以从另一个类继承函数和指针。
我有一个名为 Globals
的类,它包含指向其他类的对象的各种指针以及各种函数。我将编写一个简单的示例,而不是复制/粘贴代码:
(为了简单和干净,我移除了头部防护)
下面分别是我的globals.h和globals.cpp:
// Example of globals.h
#include <iostream>
#include <cstdio>
using namespace std;
class Globals {
public:
Globals ();
virtual ~Globals ();
void function1(char*);
void function2();
class Input *input;
class Error *error;
};
// Example of globals.cpp
#include "globals.h"
Globals::Globals()
{
input = new Input();
error = new Error();
}
void Globals::function1(char*nm)
{
cout << nm << endl;
}
现在,在我的 Input
类代码中,假设我想使用 function1(char*)
方法,如果不将对象传递给输入
类?我的意思是,目前我的 Input
类被传递了一个 *globals
对象,所以我可以像这样调用函数:globals->函数 2();
。但是如果我在不同的类中有很多函数,这会变得非常困惑。此外,有没有一种方法可以使用 Error
指针指向在 Globals
中初始化的对象?如果 Error
有一个名为 error_func()
的函数,我怎么能这样调用它:error->error_func()
从内部我的 Input
函数?
谢谢,如果我的问题太困惑,我深表歉意。如果需要,我很乐意详细说明。
阿米特
编辑 1:添加了一个简化的代码,以更清晰的方式呈现我想做的事情
// Example of globals.h
#include <iostream>
#include <cstdio>
#include "input.h"
#include "error.h"
using namespace std;
class Globals {
public:
Globals ();
virtual ~Globals ();
class Input *input;
class Error *error;
};
// Example of globals.cpp
#include "globals.h"
Globals::Globals()
{
input = new Input();
error = new Error();
}
// Example of input.h
#include "globals.h"
class Input {
public:
Input();
virtual ~Input();
}
// Example of input.cpp
#include "globals.h"
Input::Input()
{
error->print("Hello\n"); // <-- THIS is really what I want to accomplish (without being sent a globals object and say globals->error->print();
}
// Example of error.h
#include "globals.h"
class Error {
public:
Error() { }
virtual ~Error() { }
void print(char*);
}
// Example of error.cpp
#include "globals.h"
Error::print(char* nm)
{
cout << nm << endl;
}
最佳答案
如果我对你的问题的理解是正确的,函数会自动“继承”,至少对于你需要的目的是这样。
例如,您的全局类有两个方法,function1(char*)
和 function2()
。如果你上课:
class Descendent
: public Global
{ };
int main()
{
Global * global = new Global();
Global * desc = new Descendant();
char * str = "string";
// These two will run the same function:
global->function1(str);
desc->function1(str);
}
为了防止这种情况(根据当前类型调用函数),您必须使用虚拟,例如:
class Global
{
virtual void function1(char *);
};
class Descendant
{
virtual void function1(char *);
};
int main()
{
Global * global = new Global();
Global * desc = new Descendant();
char * str = "string";
// These two will NOT run the same function:
global->function1(str);
desc->function1(str);
}
现在,我不完全确定,但是这里可能会使用单例惯用语,具体取决于您的 Global
的全局化程度。在这种情况下,您将拥有一个全局的:
class Global
{
static Global * GetSingleton()
{
if (!Global::m_Instance) Global::m_Instance = new Global();
return Global::m_Instance;
}
void function1(char *);
static Global * m_Instance;
};
class Descendant
{
void function1(char *)
{
Global * global = Global::GetGetSingleton();
// ...
}
};
有多种方法可以处理类之间需要的全局变量和函数。其中之一可能是它,具体取决于您在做什么。如果没有,我会尝试编辑并推荐一个可行的。
关于c++ - C++中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7508889/