C ++ using a class from another file

I am trying to write a series of programs that use the same main file (main.cpp), but with different source files (object1.cpp, object2.cpp, etc.). Therefore, I will compile them mainly like this:

g++ -o program1.exe main.cpp object1.cpp g++ -o program2.exe main.cpp object2.cpp 

I want objectN.cpp to define a class with some implemented methods that will be called from the main file. The source codes look something like this:

header file (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

 #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 will have a similar structre for object1.cpp, but the class will have different data members.

I can't get this to work because I need to include the declaration of the MyObject class in main.cpp. But each object * .cpp file will declare a MyObject class with different members, so I can’t just create a separate object.hpp header and include it in main, because I need different headers for different objects. The main thing you need to know about is methods.

I hope I have clearly explained the problem; if you do not leave a comment, and I will try to clarify. There seems to be a really easy way to do something like this, but I can't figure it out.

thanks

+4
source share
2 answers

can't make this work because I need to include the declaration of the MyObject class in main.cpp

NOT. Make factory way

.

* h:

 MyObjectInterface* makeObject(); 
.

* castes:

 MyObjectInterface* makeObject(){ return new MyObject; } 

However, if identically named classes with the same structure are declared in different files, some compilers may confuse under certain circumstances (rarely, but this can happen).

Also, the best solution would be to rethink the structure of your program. Since your derived classes must have different data members, this means that you may have to access these data members. Since you probably need to access these memebrs, you will need to learn something about this class, so you will need to put its declaration in the header.

Another problem is that the class is a concept, so choosing a unique name should be pretty trivial. If you have many different classes called SAME, you may have a design problem - your classes do not represent unique concepts, and you create too many classes unnecessarily.

+5
source

One possible solution is to declare this function in object.hpp :

 MyObjectInterface * createInstance(); 

and implement it in two .cpp files:

 MyObjectInterface * createInstance() { return new MyObject(); } 

and replace

 x = new MyObject(1); 

by

 x = createInstance(); 
+3
source

Source: https://habr.com/ru/post/1415264/


All Articles