I have the following layout:
MainWindow <--------- Settings
\
\
V
PerformOps
MainWindow takes in variables passed from Settings, and passes them on to PerformOps.
If the Settings class is not used, MainWindow passed on defaults to PerformOps.
Currently I do:
class Settings{
public:
Settings(*parent);
var1, var2, var3
.
.
void exec();
}
class PerformOps{
public:
PerformOps();
var1, var2, var3;
.
.
void start();
}
class MainWindow{
private:
Settings *set; //share pointers over all methods
PerformOps *op;
bool settings_not_clicked = false;
void saveChanges()
{
QSettings settings("my","app");
settings.setValue("value1", op->var1);
settings.setValue("value2", op->var2);
settings.setValue("value3", op->var3);
}
void loadChanges()
{
QSettings settings("my","app");
op->var1 = settings.value("value1");
op->var2 = settings.value("value2");
op->var3 = settings.value("value3");
}
void closeEvent(QCloseEvent *event)
{
event->ignore();
saveChanges();
event->accept();
}
void takeDataAndDoStuff() //This is always called
{
op = new PerformOps;
if(settings_not_clicked) {
loadChanges()
}
else {
op->var1 = 33;
op->var2 = "Geronimo!";
op->var3 = true;
}
op->start();
}
void getStuff_maybe_if_clicked() //This might not be always called
{
Settings *set = new Settings(this);
set->exec() //Pause parent, run Settings
settings_not_clicked = false;
}
Question: Is there a cleaner way to share data across classes without resorting to the dirty method of : op->var1 = set->var1;, taking into account that the set pointer might not always be initialised?
Well, the approach itself is not that bad, however there are some things you can improve.
First of all, if I understood you correctly, you want to pass the settings if they exist, and pass the default values if they don't. In this case you can utilize the constructor:
class PerformOps
{
public:
PerformOps( int v1 = 33, string v2 = "Geronimo!", bool v3 = true ): var1(v1), var2(v2), var3(v3)
{
}
<...>
}
Now, if you call the constructor as PerformOps(), the default values will be set. If you call it and feed it some of the values, it will use them:
<...>
PerformOps *op;
if(settings_not_clicked) {
op = new PerformOps( set->var1, set->var2, set->var3 );
}
else {
op = new PerformOps();
}
<...>
of course, if you don't want to do it via the cunstructor, you could just make a function and call it like "setData()" and use the same technique with default function parameters.
Now, as for the pointers. It is a good idea to always initialize pointers with NULL, or nullptr if you have c++0x. Also, when you delete the memory, assign the pointer to NULL or nullptr again. This way you will be able to always see if the pointer is valid by a simple check.
someClass * ptr = nullptr;
<...>
ptr = new someClass();
<...>
delete ptr;
ptr = nullptr;
UPD
I would suggest you to get rid of your Settings class, and just use QSettings directly. You will not need to mess with the pointers, and reading/writing to QSettings is very fast.
Now, also don't forget that you can use QSettings from the heap:
QSettings * settings = new QSettings();
If you want each settings set to have a "parent", you could just derive your Settings class from QSettings, and just add one parent field into it. This will keep all the functionality of QSettings, which is very convenient.
Actually, your approach is fine too, all you need to do is just to check if the pointers are valid.
Ok, so the problem is "I'm setting a pointer to a pointer that may or may not exist". In such a case Null Object pattern should help. This way you should be able getting rid of if-clause
Oh hang on.
I just realized that I can use QSettings between classes (which is what SingerOfTheFall was clearly hinting at)
So from my Settings.cpp class I can do:
QSettings sett("my");
sett.begingroup("my_groupname");
sett.setValue("value1", var1);
sett.setValue("value2", var2);
sett.setValue("value3", var3);
sett.endGroup();
and I can retrieve this same information from ANY CLASS (duh!) by doing:
someRandomClass.cpp:
QSettings settings("my");
settings.beginGroup("my_groupname");
myvar1 = settings.value("value1");
myvar2 = settings.value("value2");
myvar3 = settings.value("value3");
settings.endGroup();
Related
I need help pushing Games Objects created in C++ into our Lua enviroment. this an example of what that would look like we have the following GameObjects being created in C++
class Player
{
public:
Player(std::string pName, int ObjID)
{
this -> name = pName;
this -> objectId = ObjID
}
std::string name;
int objectId;
};
Now our objects are all being created in C++ and we don't want to have constructors for it in our Lua scripts. I'm open to using either sol or LuaBridge, I've researched their examples and they show how to push classes with constructors and pushing functions, but is it possible to push userdata with properties instead of functions? so that in our lua script instead of doing obj:GetName() we can do obj.name
a bit more details of how we're creating objs and hope to push it:
Player* player1 = new Player("Player1", 1)
Player* player2 = new Player("Player2", 2)
Then hopefully we can push these to the environment somehow, thank you for any help!
What i tried with luabridge:
luabridge::getGlobalNamespace(this->lua_state)
.beginClass<GameObject>("GameObject")
.addProperty("Name", &GameObject::Name)
.endClass();
but as I understand this would be if I want to create new classes through Lua, not pushing classes already created. Tried something similar with sol as well.
Alrighty so it seems like I was able to figure it out using sol, after some research on the suggestions from the comments. my solution was to make a small wrapper for my gameobjects:
class Lua_GameObject
{
public:
Lua_GameObject(GameObject* obj) {
this -> real_obj = obj;
objects.push_back(this);
}
std::string GetName() const {
auto name_str = this->real_obj->GetName();
int len = strlen(reinterpret_cast<const char*>(name_str));
return std::string(name_str, name_str + len);
}
static std::vector<Lua_GameObject*> ObjectList() {
return Lua_GameObject::objects;
}
private:
GameObject* real_obj;
static std::vector<Lua_GameObject*> objects;
};
after this I use sol in a different file to expose the class and create the objects like this:
// Exposes Lua_Object to the enviroment
this->lua.new_usertype<Lua_GameObject>(
"GameObject",
// No Constructors from lua
sol::no_constructor,
// Name Property
"Name", sol::property(&Lua_GameObject::GetName),
// List of all objects
"ObjectList", &Lua_GameObject::ObjectList
);
now everytime I create a Lua_GameObject it will automatically be added to the list, just need to add a handle to delete them later:
// Creates GameObject
GameObject* obj = new GameObject("ObjName", 123);
//Creates new lua Object which will automatically add it to GameObject:ObjectList()
Lua_GameObject* lua_obj = new Lua_GameObject(obj);
thank you all for your help!
Hello guys a have a problem, that i can't access field tablica[i]->help, in generuj function, its saying that this field is not existing in class Task.
How can i achieve it ?
class Task
{
protected:
string contents;
int id_pyt;
int nr_pyt;
};
class Task4Answ : public Task
{
private:
int help;
public:
Task4Answ(string contents1, int id,int nr,int help1)
{
contents=contents1;
id_pyt=id;
nr_pyt=nr;
help=help1;
}
};
class TaskCollection
{
protected:
Task *collection[60];
public:
friend class Generator;
TaskCollection()
{
collection[0] = new Task4Answ("Ile jest por roku w Polsce? \na) 1 \nb) 2 \nc) 3 \nd) 4",1,0);
collection[1] = new Task4Answ("Kto wygral tegoroczny Roland Garros? \na) Federer \nb) Djokovic \nc) Nadal \nd) Thiem",1,1);
class Generator
{
protected:
Task *tablica[10];
TaskCollection T1;
public:
Generator(){}
void Generuj()
{
if(T1.collection[x]->id_pyt==1)
{
tablica[i]=new Task4Answ("0",0,0);
tablica[i]->contents=T1.collection[x]->contents;
tablica[i]->id_pyt=T1.collection[x]->id_pyt;
tablica[i]->nr_pyt=T1.collection[x]->nr_pyt;
tablica[i]->help=T1.collection[x]->help; //here is the problem
}
}
}
Or maybe there is some other solution of the project im doing now.
Thanks for any help.
The problem is in this line:
tablica[i]=new Task4Answ("0",0,0);
Although you have called the Task4Answ constructor, you are also assigning the memory address returned by new to a Task pointer. Effectively, you have casted the Task4Answ pointer to a Task pointer. On the lines that follow, C++ only sees tablica[i] as a reference to a Task pointer. You need to change:
protected:
Task *tablica[10];
TaskCollection T1;
...to this:
protected:
Task4Answ *tablica[10]; // Task was changed to Task4Answ
TaskCollection T1;
That should allow C++ to see tablica as an array of Task4Answ pointers instead of Task pointers.
Edit: it looks like help is also private. You will have to change help to public or add TaskCollection::TaskCollection() as a friend. Otherwise, C++ will not let you get or set help.
Edit: the OP added that tablica[i] might contain instances of other classes that inherit from Task. In that case, you could do something like this:
void Generuj()
{
if(T1.collection[x]->id_pyt==1)
{
Task4Answ* newTask = new Task4Answ("0",0,0);
newTask->contents=T1.collection[x]->contents;
newTask->id_pyt=T1.collection[x]->id_pyt;
newTask->nr_pyt=T1.collection[x]->nr_pyt;
newTask->help=T1.collection[x]->help; // You will still have to change this from being private.
tablica[i] = newTask;
}
}
}
Later on, in order to access help, you will need to implement some sort of way of checking whether tablica[i] is a Task4Answ and not an instance of some other class that inherits from Task, perhaps by implementing a method in Task named IsTask4Answ that returns false in Task but is overridden to return True in Task4Answ. You can then cast the pointer back to Task4Answ with something like the static_cast operator. In other words:
// Add these functions to the class definitions:
virtual bool Task::IsTask4Answ() const {
return false;
}
bool Task4Answ::IsTask4Answ() const override {
return true;
}
// Later, you can do this:
if(tablica[i].IsTask4Answ()){
Task4Answ* t = static_cast<Task4Answ*>(tablica[i]);
t->help; // Again, you'll have to change this from being private.
}
Although I suggest figuring out a different data structure where you do not need to do any casting, this will allow you to access help.
Do note the virtual keyword in the first function above; it allows the function to be dynamically bound, which means that the code will check whether to call Task::IsTask4Answ() or Task4Answ::IsTask4Answ() at runtime instead of at compile time.
I've tried to solve my problem for 2 days now and failed miserably. Internet does not help.
What I'm trying to do is to communicate two classes which reside within another class.
This is my first "big" project so I assume my design is terrible for you guys.
Also, my program is split between a lot of files which may be confusing.
Lets hit it! For the sake readability, I've changed every member to public.
This is my MainOGLController class which is the main class that controls everything my program does:
class MainOGLController
{ // I deleted constructor/destructor from this quote
public:
DisplayController* Display;
StellarManager* Manager; // it will need to use something from Display
void RenderScene();
bool CreateNewDisplay(int, char*[]); // argc argv
}
Ok, this is how i create instance of this class in file with main():
#include "MainOGLController.h"
MainOGLController Controller;
int main(int argc, char* argv[])
{
if ( Controller.CreateNewDisplay(argc, argv) ) return 1; // if it fails then exit;
// some opengl code here
return 0;
}
Now you are probably wondering how does the CreateNewDisplay method look like:
bool MainOGLController::CreateNewDisplay(int argc, char* argv[])
{
Display = new DisplayController(argc, argv);
Manager = new StellarManager(&Display); // me trying to make reference to Display
// to be able to use it within Manager
//ogl code
else return 0;
}
OK, so I'm creating Manager there and now we should see how i created the StellarManager class:
class StellarManager
{
std::vector<Stellar*> VectorManager; // objects from this vector will need to use
// ptrDisplay to access Display
DisplayController* ptrDisplay;
StellarManager(DisplayController&);
void addEntity();
};
Now for the constructor:
StellarManager::StellarManager(DisplayController& _p) // me trying to do anything
{
*ptrDisplay = _p;
}
So at this point I should have instance of MainOGLController, and within it, a pointer to DisplayController and StellarController, where StellarController should have its own pointer to the same DisplayController.
Now somewhere withing working piece of code I'm calling the addEntity method:
void StellarManager::addEntity()
{
VectorManager.push_back(new Stellar(&ptrDisplay); // sending ptrDisplay so that the
// Stellar object can use it
}
Stellar class is defined like this:
class Stellar
{
public:
DisplayController* ptrDisplay;
Stellar(DisplayController**);
void Draw(); // finally, heres where i want to use this Display pointer
};
Stellar constructor:
Stellar::Stellar(DisplayController** _p)
{
*ptrDisplay = **_p;
}
OKAY! Thats the final piece. All i want to do now is simply call method Draw which belongs to Stellar class and use Display which is located in MainOGLController.
Manager->VectorManager[0].Draw();
Oh and the Draw looks just like this:
void Stellar::Draw(int _mode)
{
GLMatrixStack* mvm = &(ptrDisplay->modelViewMatrix);
mvm->Scale(2, 0.5, 0.5); // Scale is a method from GLMatrixStack
}
Thats all folks, if theres any better way of doing this, im all ears.
What I did does not work, I'm able to use the *ptrDisplay from Stellar class but nothing happens so I guess I'm not using its reference but a copy.
Sorry, I know this is a lot of code and it may be very confusing. I just dont know what to do now...
It looks like the problem is here:
Stellar::Stellar(DisplayController** _p)
{
*ptrDisplay = **_p;
}
You're dereferencing a pointer (ptrDisplay) that was never initialized. This results in undefined behavior. I think this captures what you wanted to do:
Stellar::Stellar(DisplayController* _p) : ptrDisplay(_p)
{
}
It's not necessary to pass a pointer-to-pointer-to-DisplayController; all your Stellar class needs is a pointer to a DisplayController. Moreover, it sounds like you don't want to dereference _p and copy it, so simply copying the pointer (via ptrDisplay(_p)) will result in ptrDisplay pointing to the same object as _p.
My goal is to pass the windowobject pointer to another class.
I'll show you what i got so far.
where: "dialog" is the window to pass.
mainwindow.cpp
dialog = new Dialog(this);
someClass(dialog);
Konstruktor in someClass
someClass::someClass(Dialog *d)
{
Dialog *dia = d;
}
someClass.h
#include "dialog.h"
...
public:
someClass(Dialog *dialog)
//Dialog d;
The program runs now, but i'm not sure if i achieved what i wanted.
Is it possible to interact with my dialog now?
What i want is something like this.
dia->ui->lineEdit->setText();
Any help would be appriciated
someClass(&dialog);
is incorrect ... you have a pointer and give the address of the pointer (a pointer to the pointer) in the function
also you have
Dialog d;
in your header and assigning a Dialog* to it.
I recommend you to have a look at: The Definitive C++ Book Guide and List
My goal is to pass the windowobject pointer to another class. I'll
show you what i got so far. where: "dialog" is the window to pass.
considering your code:
someClass::someClass(Dialog *d)
{
Dialog *dia = d;
}
is a local member in the constructor someClass. Therefore it only has scope in the constructor itself (is not visible outside of the constructor, and in fact, does not live outside of the constructor (is destroyed when the constructor goes out of scope)).
Fortunately dia is a pointer (address of object), and not the actual dialog (therefore only the pointer, and not the object that it points to goes out of scope). If you want the pointer to remain in scope for the purpose of access later on, you have to "tie" it to the scope of the class (make it a class member).
class MyClass
{
public:
//Using reference as it may not be null...
MyClass( Dialog& dialog );
void showDialog();
private:
//We only want to expose a small part of dialog to users,
// hence keep it private, and expose what we want through
// the interface (the public part).
Dialog& dialog_;
};
//MyClass.cpp
MyClass::MyClass( QPointer<Dialog> )
: dialog_( dialog ) //Google "member initialisation"
{
}
void MyClass::showDialog(){ dialog_.show(); }
----- Modified/Additional answer -----
If in the above example dialog_ is optional, then you needn't make it a reference member, as reference members require to be initialised (one cannot have an uninitialised reference). In that case, make it a pointer... When using Qt, I would make it a QPointer (Assuming Dialog is a QObject), as QPointers are safer to work with than raw pointers (They are always initialised to zero, at least).
I'll show you the basic principle to keep it simple for now. Read up about QPointers and smart pointers in general.
e.g:
class MyClass
{
public:
// May or may not hold zero...
explicit MyClass( Dialog* dialog = 0 );
void showDialog();
private:
//We only want to expose a small part of dialog to users,
// hence keep it private, and expose what we want through
// the interface (the public part).
Dialog* dialog_;
};
//.cpp
MyClass::MyClass( Dialog* dialog /* = 0*/ )
: dialog_( dialog )
{
}
void MyClass::showDialog()
{
if( dialog_ )
{
dialog_->show();
}
else
{
std::cout << "This is in fact not a dialog"
"\nbut be so kind as to enter"
" whatever you want here ;-)"
<< std::endl;
while( !terminated() )
{
std::string inputStr;
std::cin >> inputStr;
evalute( inputStr );
}
}
}
We don't know what your Dialog class looks like, but if its member ui is public (or someClass is a friend of Dialog), then you should be able to do
dia->ui->lineEdit->setText();
Do you get any compiler errors? Or does the text simply not show up as expected?
You would still need to show the dialog at some point using
dia->show();
or
dia->exec();
For a class, which is only defined in a header, I need a special behavior of one method for all instance of the class. It should be depending on a default value, which can be changed any time during runtime. As I do not want a factory class nor a central management class I came up with that idea:
class MyClass
{
public:
void DoAnything() // Methode which should be act depending on default set.
{
// Do some stuff
if(getDefaultBehaviour())
{
// Do it this way...
}
else
{
// Do it that way...
}
}
static bool getDefaultBehaviour(bool bSetIt=false,bool bDefaultValue=false)
{
static bool bDefault=false;
if(bSetIt)
bDefault=bDefaultValue;
return bDefault;
}
};
It works, but it looks a little awkward. I wonder if there is a better way following the same intention.
In the case where I want to use it the software already created instances of that class during startup and delivered them to different parts of the code. Eventually the program gets the information how to treat the instances (for e.g. how or where to make themselves persistent). This decision should not only affect new created instances, it should affect the instances already created.
I'd advise to use a simple method to simulate a static data member, so the usage becomes more natural:
class MyClass
{
public:
// get a reference (!) to a static variable
static bool& DefaultBehaviour()
{
static bool b = false;
return b;
}
void DoAnything() // Methode which should be act depending on default set.
{
// Do some stuff
if(DefaultBehaviour())
{
// Do it this way...
}
else
{
// Do it that way...
}
}
};
where the user can change the default at any time with
MyClass::DefaultBehaviour() = true;
My thanks to Daniel Frey with his answer which I already marked as the best. I wanted to add my final solution which is based on the answer from Frey. The class is used by some c++ beginners. As I told them to use always getter and setter methods, the way described by Frey looks very complex to beginners ("uuuh, I can give a function a value?!?!"). So I wrote the class like followed:
class MyClass
{
public:
// get a reference (!) to a static variable
static bool& getDefaultBehaviour()
{
static bool b = false;
return b;
}
static void setDefaultBehaviour(bool value)
{
getDefaultBehaviour()=value;
}
void DoAnything() // Methode which should be act depending on default set.
{
// Do some stuff
if(getDefaultBehaviour())
{
// Do it this way...
}
else
{
// Do it that way...
}
}
};
for the user, I looks now like a usual getter and setter.