For a c++ application which I'm currently being busy to develop, I have several classes which I need to access through my entire code, without creating a new object
So searching I have found that one of methods that can be used is with the extern linkage specifier.
I would like to know what is best way to use this extern method, I wrote a little sample code
classone.h
#ifndef CLASSONE_H
#define CLASSONE_H
class ClassOne
{
public:
ClassOne();
void showClassOneInt();
private:
int m_classOneInt;
};
extern ClassOne *classOne;
---------------------------------------
classone.cpp
#include "classone.h"
#include <QDebug>
ClassOne *classOne;
ClassOne::ClassOne()
{
m_classOneInt = 1;
}
void ClassOne::showClassOneInt()
{
qDebug() << "ClassOneInt: " << m_classOneInt;
}
---------------------------------------
classtwo.h
#ifndef CLASSTWO_H
#define CLASSTWO_H
class ClassTwo
{
public:
ClassTwo();
void showClassTwoInt();
private:
int m_classTwoInt;
};
#endif // CLASSTWO_H
---------------------------------------
classtwo.cpp
#include "classtwo.h"
#include <QDebug>
ClassTwo::ClassTwo()
{
m_classTwoInt = 2;
}
void ClassTwo::showClassTwoInt()
{
qDebug() << "ClassTwoInt: " << m_classTwoInt;
}
---------------------------------------
classthree.h
#ifndef CLASSTHREE_H
#define CLASSTHREE_H
class ClassThree
{
public:
ClassThree();
void showClassThreeInt();
private:
int m_classThreeInt;
};
#endif // CLASSTHREE_H
---------------------------------------
classthree.cpp
#include "classthree.h"
#include <QDebug>
ClassThree::ClassThree()
{
m_classThreeInt = 3;
}
void ClassThree::showClassThreeInt()
{
qDebug() << "ClassThreeInit: " << m_classThreeInt;
}
---------------------------------------
classtest.cpp
#include "classtest.h"
#include "classone.h"
#include "classtwo.h"
#include "classthree.h"
//Class one pointer already in header
//Class two
extern ClassTwo *classTwo;
//Class three
extern ClassThree *classThree;
ClassTest::ClassTest()
{
//Execute class one
classOne->showClassOneInt();
//Execute class two
classTwo->showClassTwoInt();
//Execute class three
classThree->showClassThreeInt();
}
---------------------------------------
main.cpp
#include <QCoreApplication>
#include "classone.h"
#include "classtwo.h"
#include "classthree.h"
#include "classtest.h"
//Class one pointer already in header file
//Class two pointer
ClassTwo *classTwo;
//Class three pointer
ClassThree *classThree;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Create object for class one
classOne = new ClassOne;
//Create object for class two
classTwo = new ClassTwo;
//Create object for class three
ClassThree three;
classThree = &three;
//Create a classTest object
ClassTest test;
return a.exec();
}
Please could you tell me what is the best way, thanks for you help.
The best way is to not do it and instead use dependency injection.
If you choose to do it anyway, you should at least use getter/factory functions (i.e. ClassOne &getClassOne())) so you can:
be sure random code can't change the objects and
handle order of construction implicitly by constructing on first use (sometimes appropriate, sometimes not).
Having a global state is generally not a great idea, seek to eliminate it.
If that cannot be done, try the singleton pattern.
class Singleton
{
Singleton(); //keep constructors private to avoid creation by others
static Singleton inst;
public:
static Singleton& Instance() {return inst;}
};
Singleton Singleton::inst;
Related
How can I get one class to run another class' function and vice versa C++?
Example:
main.h (Updated/edited)
#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include "test.h"
class firstClass
{
public:
void runMe();
};
extern firstClass first;
#endif
main.cpp (Updated/edited)
#include "main.h"
firstClass first;
secondClass second;
void firstClass::runMe()
{
std::cout << "I\'m in firstClass\n";
}
int main()
{
second.runMe(); // should output "I'm in secondClass" then "I'm in firstClass"
return 0;
}
test.h (Updated/edited)
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include "main.h"
class secondClass
{
public:
void runMe();
};
extern secondClass second;
#endif
test.cpp (Updated/edited)
#include "test.h"
firstClass first;
secondClass second;
void secondClass::runMe()
{
std::cout << "I\'m in secondClass\n";
first.runMe(); // Now we output the other classs runMe, saying "I'm in firstClass"
}
I really need help, and badly!
I'm self taught and really need this program to work, its for a friend who needs it soon.
Edit: I changed the code, and now I get
/tmp/test-034790.o:(.bss+0x1): multiple definition of `first'
/tmp/main-097c28.o:(.bss+0x1): first defined here
/tmp/test-034790.o:(.bss+0x2): multiple definition of `second'
/tmp/main-097c28.o:(.bss+0x2): first defined here
I'm tinkering a bit with Qt's meta-object system, and I've come across an issue with adding enum class to a meta-object. I have a struct that contain some variables, one of which is an enum class.
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <QObject>
enum class CarType {
NO_CAR = 0,
SLOW_CAR,
FAST_CAR,
HYPER_CAR
};
struct Player {
Q_GADGET
Q_PROPERTY(Player player READ getPlayer)
Q_PROPERTY(float m_speed READ getSpeed)
Q_PROPERTY(CarType m_carType READ getCarType)
Player getPlayer() { return *this;}
float getSpeed() {return m_speed;}
CarType getCarType() { return m_carType;}
public:
CarType m_carType;
float m_speed;
}; Q_DECLARE_METATYPE(Player)
#endif // EXAMPLE_H
I declare this struct as a Q_META_TYPE, in order to access it with the meta-object system. This allows me to access the struct's properties. Here is my main.cpp:
#include <QCoreApplication>
#include <iostream>
#include <QMetaObject>
#include "example.h"
#include <QDebug>
#include <QMetaProperty>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qRegisterMetaType<Player>();
const QMetaObject* metaObject = &Player::staticMetaObject;
qInfo()<< "Number of enums is: " << metaObject->enumeratorCount();
return a.exec();
}
I expect that enumeratorCount() would be 1, however I get 0. I've searched about this issue, and most examples I could find have the struct inside a class, and the Q_ENUM declaration right after it, like this:
enum class CarType {
NO_CAR = 0,
SLOW_CAR,
FAST_CAR,
HYPER_CAR
}; Q_ENUM(CarType)
This, however, results in an error. What's the proper way to register this enum in the meta-object system?
Thanks for your time.
EDIT
The errors that I get that are relevant are:
/home/raphael/SO_Example/example.h:10: error: ‘friend’ used outside of class
/home/raphael/SO_Example/example.h:10: error: ‘staticMetaObject’ was not declared in this scope; did you mean ‘qt_getQtMetaObject’?
If you don't want to add your enum to a class with Q_OBJECT or Q_GADGET, then the other way to do it is to put your enum inside a Q_NAMESPACE. You can't actually use Q_ENUM with Q_NAMESPACE, but you can use Q_ENUM_NS instead. I haven't tested this myself, but it should work:
namespace MyNamespace
{
Q_NAMESPACE
enum class MyEnum {
Foo,
Bar
};
Q_ENUM_NS(MyEnum)
}
(I have read all the threads posted here and google, I was not able to fix from that)
I am having toruble with a incomplete type error when compiling. The way I am designing the project, the game pointer is unavoidable.
main.cpp
#include "game.h"
// I actually declare game as a global, and hand itself its pointer (had trouble doing it with "this")
Game game;
Game* gamePtr = &game;
game.init(gamePtr);
game.gamePtr->map->test(); // error here, I also tested the basic test in all other parts of code, always incomplete type.
game.h
#include "map.h"
class Map;
class Game {
private:
Map *map;
Game* gamePtr;
public:
void init(Game* ownPtr);
int getTestInt();
};
game.cpp
#include "game.h"
void Game::init(Game* ownPtr) {
gamePtr = ownPtr;
map = new Map(gamePtr); // acts as "parent" to refer back (is needed.)
}
int Game::getTestInt() {
return 5;
}
map.h
class Game;
class Map {
private:
Game* gamePtr;
public:
int test();
};
map.cpp
#include "map.h"
int Map::test() {
return gamePtr->getTestInt();
}
// returns that class Game is an incomplete type, and cannot figure out how to fix.
Let's go over the errors:
1) In main, this is an error:
game.gamePtr->map->test();
The gamePtr and map are a private members of Game, therefore they cannot be accessed.
2) The Map is missing a constructor that takes a Game* in Game.cpp.
map = new Map(gamePtr);
Here is a full working example that compiles. You have to provide the functions that are missing bodies, such as Map(Game*).
game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
class Map;
class Game {
private:
Map *map;
public:
Game* gamePtr;
void init(Game* ownPtr);
int getTestInt();
};
#endif
game.cpp
#include "game.h"
#include "map.h"
void Game::init(Game* ownPtr) {
gamePtr = ownPtr;
map = new Map(gamePtr); // acts as "parent" to refer back (is needed.)
}
int Game::getTestInt() {
return 5;
}
map.h
#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED
class Game;
class Map {
private:
Game* gamePtr;
public:
int test();
Map(Game*);
};
#endif
map.cpp
#include "game.h"
#include "map.h"
int Map::test() {
return gamePtr->getTestInt();
}
main.cpp
#include "game.h"
#include "map.h"
int main()
{
Game game;
Game* gamePtr = &game;
game.init(gamePtr);
game.gamePtr->map->test();
}
After doing this and creating a project in Visual Studio, I do not get any errors building the application.
Note the usage of #include guards, which your original posted code did not have. I also placed the members that were private and moved them to public in the Game class, so that main() can compile successfully.
You need to use forward declaration. Place declaration of Map class before definition of class Game:
game.h
class Map; // this is forward declaration of class Map. Now you may have pointers of that type
class Game {
private:
Map *map;
Game* gamePtr;
public:
void init(Game* ownPtr);
int getTestInt();
};
Every place where you use Map and Game class by creating instance of it or dereferencing pointer to it either through -> or * you have to make that type "complete". It means that main.cpp must include map.h and map.cpp must include game.h directly or indirectly.
Note you forward declare class Game to avoid game.h to be included by map.h, and that is fine and proper, but map.cpp must have game.h included as you dereference pointer to class Game there.
IClass (My interface):
#ifndef _ICLASS_H
#define _ICLASS_H
#include <sstream>
namespace Test
{
class __declspec(dllexport) IClass
{
public:
virtual ~IClass() {}
virtual bool Init(const std::string &path) = 0;
};
}
#endif
Class.h
#ifndef _CLASS_H
#define _CLASS_H
#include "IClass.h"
#include <memory>
#include <sstream>
#include <stdio.h>
namespace Test
{
class Class: public IClass
{
public:
Class();
~Class();
bool Init(const std::string &path);
};
}
#endif
Class.cpp
#include "Class.h"
namespace Test
{
Class::Class()
{
}
bool Class::Init(const std::string &path)
{
try
{
// do stuff
return true;
}
catch(std::exception &exp)
{
return false;
}
}
}
main (in exe, dll linked implicitly)
#include "IClass.h"
using namespace Test;
int main(int argc, char* argv[])
{
std::shared_ptr<IClass> test = std::make_shared<Class>(); // error: unreferenced Class
test->Init(std::string("C:\\Temp"));
}
At the moment Class is not declared
-> if I include Class.h to main following error occurs: LNK2019: unresolved external symbol: add class __declspec(dllexport) Class : public IClass resolve this linker issue, but is it ok to do it this way?
-> I also can't do this: std::shared_ptr<IClass> test = std::make_shared<IClass>();
(because it's not allowed to create an object of abstract class)
How can I solve this issue and is this best practise?
If you want your EXE to allocate a new "Class" object, the EXE code has to know the Class type. If you want to keep the Class type unknown from the EXE, one solution may be to export from your DLL a factory function, which will construct a Class object and return it as an IClass pointer.
See How to implement the factory pattern in C++ correctly
OK so i have a few problems.
1. I need to be able to register the childclass when its created with game for broadcasting.
2. I need to be able to call the child class's method from an array of GameObject Pointers.
3. I need it to be the actual classes, not just copys of them.
In the file systemvars.h i have my global methods and some variables.
#pragma once
//=================================
// include guard
#ifndef _SYSTEMVARS_H_
#define _SYSTEMVARS_H_
//=================================
// forward declared dependencies
class Game;
//=================================
// included dependencies
#include <iostream>
using namespace std;
//global enums
const enum CLASSTYPE{CLASSTYPE_NULL,CLASSTYPE_PLAYER,CLASSTYPE_DUNGION,CLASSTYPE_ENTITY,CLASSTYPE_MAP,CLASSTYPE_MENU};
//Global methods
void setGameRefrence(Game*);
Game* getGameRefrence();
systemvars.cpp: definitions
#include "SystemVars.h"
static Game* curentgame;
void setGameRefrence(Game* mygame)
{
curentgame = mygame;
}
Game* getGameRefrence()
{
return curentgame;
}
In my Game.h: a holder for gameobjects
#pragma once
//=================================
// include guard
#ifndef _GAME_H_
#define _GAME_H_
//=================================
// forward declared dependencies
class GameObject;
//class Player;
//=================================
// included dependencies
#include "SystemVars.h"
#include "GameObject.h"
//#include "Player.h"
#include <iostream>
using namespace std;
//=================================
// the actual class
class Game
{
public:
Game(void);
void registerGameObject(GameObject*);
void unregisterGameObject(GameObject*);
void sendMessageToAllObjects(string message,CLASSTYPE recipeint);
~Game(void);
private:
GameObject *gameobjects2[1000];
int numberofobject;
};
#endif
the game.cpp gameclass definition
#include "Game.h"
//#include "Player.h"
Game::Game(void)
{
setGameRefrence(this);
//logHelperMessage(INFO,1,"Registerd game");
numberofobject = 0;
}
void Game::registerGameObject(GameObject* newobj)
{
//logHelperMessage(INFO,1,"Registerd");
newobj->setId(numberofobject);
gameobjects2[numberofobject] = newobj;
numberofobject++;
//gameobjects.push_back(newobj);
}
void Game::unregisterGameObject(GameObject* objtodie)
{
//logHelperMessage(INFO,1,"Unregister");
for(int i = objtodie->getId();i < numberofobject - 1;i++)
{
gameobjects2[i] = gameobjects2[i+1];
gameobjects2[i]->setId(i);
}
gameobjects2[numberofobject-1] = nullptr;
numberofobject--;
}
void Game::sendMessageToAllObjects(string message,CLASSTYPE recipeint)
{
for(int i = 0; i < numberofobject;i++)
{
cout << "Sent the message from game");
//((Player *)gameobjects2[i])->sendMessage(message);
//static_cast<Player*>(gameobjects2[i])->sendMessage(message);
}
}
Game::~Game(void)
{
}
Gameobject.h: the parent of my inner game classes.
#pragma once
//=================================
// include guard
#ifndef _GAMEOBJECT_H_
#define _GAMEOBJECT_H_
//=================================
// forward declared dependencies
enum CLASSTYPE;
//=================================
// included dependencies
#include <iostream>
#include "SystemVars.h"
using namespace std;
//=================================
// the actual class
class GameObject
{
public:
GameObject();
GameObject(CLASSTYPE mytype);
~GameObject(void);
virtual void sendMessage(string data);
virtual CLASSTYPE getMyClassType();
virtual void setMyClassType(CLASSTYPE newrecip);
void setId(int val);
int getId();
protected:
CLASSTYPE _MYCURRENTCLASSTYPE;
int myid;
};
#endif
Gameobject.cpp
#include "GameObject.h"
GameObject::GameObject() : _MYCURRENTCLASSTYPE(CLASSTYPE_NULL)
{
//do not register
}
GameObject::GameObject(CLASSTYPE mytype): _MYCURRENTCLASSTYPE(mytype)
{
//register this object into the gameobject list.
getGameRefrence()->registerGameObject(this);
}
GameObject::~GameObject(void)
{
getGameRefrence()->unregisterGameObject(this);
}
void GameObject::sendMessage(string data)
{
//logHelperMessage(INFO,1,"Recived te message in GameObject");
cout << "Recived te message in GameObject";
}
CLASSTYPE GameObject::getMyClassType()
{
return _MYCURRENTCLASSTYPE;
}
void GameObject::setMyClassType(CLASSTYPE newrecip)
{
}
void GameObject::setId(int val)
{
myid = val;
}
int GameObject::getId()
{
return myid;
}
Player.h:
pragma once
//=================================
// include guard
#ifndef _PLAYER_H_
#define _PLAYER_H_
//=================================
// forward declared dependencies
//=================================
// included dependencies
#include "SystemVars.h"
#include "GameObject.h"
//=================================
// the actual class
class Player : public GameObject
{
public:
Player();
void sendMessage(string data) override;
void test();
}
Player.cpp:
Player::Player() : GameObject(CLASSTYPE_PLAYER)
{
}
void Player::sendMessage(string data)
{
//logHelperMessage(INFO,1,"Recived the message in Player");
cout << "Recived the message in Player";
//logHelperMessage(INFO,1,data.c_str());
cout << data;
}
void Player::test()
{
cout << "Sent message";
getGameRefrence()->sendMessageToAllObjects("Test",CLASSTYPE_PLAYER);
}
main.cpp
#pragma once
#include "SystemVars.cpp"
#include "Player.h"
#include "Game.h"
Game mygame;
int main(int argc, char **argv)
{
setGameRefrence(&mygame);
Player newplayer = Player();
newplayer.test();
}
Now that that is all out of the way.
The expected output is:
Sent message
Sent message from game
Recived message in Player
but insted i get:
Sent message
Sent message from game
Recived message in Gameobject
Im pretty sure i have a sliceing problem, but im not sure what to do about it, or where it is.
So, any ideas gents?
ALso, i tried to cut back the classes a bit so im not posting 2000~ lines of code. So if anyhting missing let me know.
About a dozen classes inherit from gameobject. and i need them all to talk to eachother in one way or another.
Just by having CLASSTYPE_PLAYER, etc. makes me concerned that you're trying to subvert/reinvent C++ OOP. You already have the concept of 'class' in the language, why re-invent it? Forgive me if I'm wrong. Aside from this, I suggest you look up publisher-subscriber frameworks to do the kind of thing you're after, or implement your own. I'm sure somebody will answer your C++ issue more directly.
Edit: tone of first comment.