PDA

View Full Version : main.cpp variable access question



MarkoSan
9th March 2008, 22:12
Hi to all!

Can somene please tell me how to access variable appSettings, declared in main.cpp:
// qt includes
#include <QtGui>
#include <QApplication>
#include <QPlastiqueStyle>

// custom includes
#include "cmainwindow.h"
#include "globals.h"
#include "CApplicationSettings.h"


int main(int argc, char *argv[])
{
....
CApplicationSettings appSetings(strIniFileName,
QSettings::IniFormat,
qApp); // sets up app settings

....
}from class, that is defined through
#include "cmainwindow.h" in main.cpp? Is this possible?

wysota
9th March 2008, 22:48
No, this variable is only visible from inside main(). If you want to access it from outside, you need to provide a pointer to it either through a global variable or through some method of a class that itself has access to that variable or a static method returning the instance (just like qApp and QCoreApplication::instance() are used).

MarkoSan
9th March 2008, 23:40
hmm, then I must rewrite CApplicationSettings like I did with CDatabaseSettings: make it singleton?

Or, where should I put pointer to this variable?

wysota
10th March 2008, 06:39
You can use a global variable.

CApplicationSettings *settings;

int main(...){
CApplicationSettings Settings;
settings = &Settings;
}

extern CApplicationSettings *settings;
void xxx(){ settings->doSomething(); }

MarkoSan
10th March 2008, 16:11
But wysotta, why do I need both declarations from line 1 AND from line 8? Otherwise, I rewritten the code according to your hint, it compiles without errors. Now let me check this pointer. Thanks man!

Well, when i try to acces global variable from other class, I get this error:
mingw32-make
mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Documents and Settings/markofr/workspace/SettingsEditor'
c:\Qt\4.3.4\bin\uic.exe cmainwindow.ui -o ui_cmainwindow.h
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CDataInputWidget.o CDataInputWidget.cpp
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CClientVisualSettingsPage.o CClientVisualSettingsPage.cpp
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CLanguageSettingsPage.o CLanguageSettingsPage.cpp
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CDatabaseSettingsPage.o CDatabaseSettingsPage.cpp
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CSettingsWindow.o CSettingsWindow.cpp
CSettingsWindow.cpp: In member function `void CSettingsWindow::saveSettings()':
CSettingsWindow.cpp:124: error: `pAppSettings' undeclared (first use this function)
CSettingsWindow.cpp:124: error: (Each undeclared identifier is reported only once for each function it appears in.)
mingw32-make[1]: *** [debug/CSettingsWindow.o] Error 1
mingw32-make: *** [debug] Error 2
mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/workspace/SettingsEditor'

wysota
10th March 2008, 18:04
Line 8 has to be in a file where you want to use the variable declared in the file containing line 1. Otherwise you'll get wat you got in line 11 of your compiler output.

MarkoSan
10th March 2008, 18:25
Just one more thing I want to ask about "background" of this code implementation. How does compiler knows the pointer from main and extern pointer are the same? Because of same name? And, I've read today about this topic we are discussing and it seems these global pointers are very dangerous in program, because it's value can be changed without notifiacation, which leads to possible application crash. How do I make this pointer const?

wysota
10th March 2008, 19:54
How does compiler knows the pointer from main and extern pointer are the same? Because of same name?
Yes, the name and the extern keyword.


And, I've read today about this topic we are discussing and it seems these global pointers are very dangerous in program, because it's value can be changed without notifiacation, which leads to possible application crash.
Correct. But if you're the only programmer and you know what you are doing, you can safely use it.


How do I make this pointer const?

You can't. As an alternative you can implement the singleton pattern for the class.

MarkoSan
10th March 2008, 20:04
Wysota, thank you very very much for clarification of this topic. Just one final question: What are other design patterns beside singleton design pattern for solving this problem? So I can google it up and read about it ...

wysota
10th March 2008, 20:25
I don't think there are other patterns that might be helpful, although I'm not much of an expert on design patterns.

MarkoSan
10th March 2008, 20:48
Ok, will google it anyway.