C++ 使用来自不同文件的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10825384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ using a class from a different file
提问by smead
I am trying to write a series of programs that use the same main (main.cpp) file, but with different secondary source files (object1.cpp, object2.cpp, etc). So I will be compiling them essentially like this:
我正在尝试编写一系列使用相同主 (main.cpp) 文件但具有不同辅助源文件(object1.cpp、object2.cpp 等)的程序。所以我将基本上像这样编译它们:
g++ -o program1.exe main.cpp object1.cpp
g++ -o program2.exe main.cpp object2.cpp
What I want to do is have objectN.cpp define a class with certain methods implemented, which will be called from the main file. The source codes look something like this:
我想要做的是让 objectN.cpp 定义一个实现了某些方法的类,这些方法将从主文件中调用。源代码如下所示:
header file (object.hpp)
头文件(object.hpp)
#ifndef INCLUDE_OBJECT_HPP
#define INCLUDE_OBJECT_HPP
class MyObjectInterface
{
public:
MyObjectInterface();
virtual ~MyObjectInterface() {};
virtual void MethodA() = 0;
virtual void MethodB() = 0;
};
#endif
object1.cpp
对象1.cpp
#include <iostream>
#include "object.hpp"
using namespace std;
class MyObject : public MyObjectInterface
{
private:
int member;
public:
MyObject(int a) { member = a; }
void MethodA() { cout << member << endl; }
void MethodB() { cout << member*2 << endl; }
};
main.cpp
主程序
#include <iostream>
#include "object.hpp"
using namespace std;
MyObjectInterface *x;
int main()
{
x = new MyObject(1);
x->MethodA();
x->MethodB();
return 0;
}
object2.cpp would have a similar structre to object1.cpp but the class would have different data members.
object2.cpp 将具有与 object1.cpp 类似的结构,但该类将具有不同的数据成员。
I can't get this to work, because I need to include the the class declaration of MyObject in main.cpp. But each object*.cpp file will declare the MyObject class with different members, so I cannot just simply make a seperate object.hpp header and include it in main, because I need different headers for different objects. The only thing main should have to know about is the methods.
我不能让它工作,因为我需要在 main.cpp 中包含 MyObject 的类声明。但是每个 object*.cpp 文件都会用不同的成员声明 MyObject 类,所以我不能简单地制作一个单独的 object.hpp 标头并将其包含在 main 中,因为我需要为不同的对象使用不同的标头。main 唯一需要知道的是方法。
I hope I've explained the problem clearly; if not leave a comment and I'll try to clarify. It seems like there should be a really simple way of doing something like this but I can't figure it out.
我希望我已经清楚地解释了这个问题;如果没有发表评论,我会尽力澄清。似乎应该有一种非常简单的方法来做这样的事情,但我无法弄清楚。
Thanks
谢谢
回答by SigTerm
can't get this to work, because I need to include the the class declaration of MyObject in main.cpp
不能让它工作,因为我需要在 main.cpp 中包含 MyObject 的类声明
NO you don't. Make a factory method
不,你没有。制作工厂方法
*.h:
*。H:
MyObjectInterface* makeObject();
*.cpp:
*.cpp:
MyObjectInterface* makeObject(){
return new MyObject;
}
However, having identically named classes with identical structure declared in different files might confuse somecompilers in certain circumstances (rare thing, but it can happen).
但是,在不同文件中声明具有相同结构的同名类可能会在某些情况下使某些编译器感到困惑(这种情况很少见,但可能会发生)。
Also, better solution would be to rethink your program structure. Because your derived classes are supposed to have different data members, it means you'll probably need to access those data members. Because you'll probably need to access those memebrs, then you'll need to know somethingabout that class, so you'll have to put its declaration into header.
此外,更好的解决方案是重新考虑您的程序结构。因为您的派生类应该具有不同的数据成员,这意味着您可能需要访问这些数据成员。因为您可能需要访问这些 memebr,所以您需要了解有关该类的一些信息,因此您必须将其声明放入header.xml 中。
Another problem is that class represents some kind of concept, so picking unique name should be fairly trivial. If you have many different classes with SAME name, you might have a design problem - your classes do not represent unique concepts, and you create too many classes needlessly.
另一个问题是类代表某种概念,因此选择唯一名称应该相当简单。如果您有许多具有相同名称的不同类,您可能会遇到设计问题 - 您的类不代表独特的概念,并且您不必要地创建了太多类。
回答by Aasmund Eldhuset
One possible solution is to declare this function in object.hpp
:
一种可能的解决方案是在以下位置声明此函数object.hpp
:
MyObjectInterface * createInstance();
and implement it like this in the two .cpp
files:
并在两个.cpp
文件中像这样实现它:
MyObjectInterface * createInstance() {
return new MyObject();
}
and replace
并替换
x = new MyObject(1);
by
经过
x = createInstance();