Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Headers in c++



  • I can't post whole project because it have more than 15 files So I will try to create a simple example. Hope it isn't wrong

    ///simple.h
    #ifndef SIMPLE
    #define SIMPLE
    #include<QString>
    #include<QFile>
    #include<QMessageBox>
    #include<QDebug>
    #include<ctime>
    #include<QTextStream>
    #include<QIntValidator>
    #include<QMap>
    class SIM{
    	
    	public:
    	SIM(){
    			// fetch some data from something like getting the username password either from file or local database
    			// assign the value to `username` and `password` like username = foundUsername;
    		};
    		int number;
    		QString username;
    		QString databasepassword;
    		// this is just demo so I am using it as public variable. 
    }
    #endif
    

    This file fetches things from file and store in username and password

    ///second.h
    // all simple.h files and extras
    #ifndef SECOND
    #define SECOND
    #include<QSql>
    #include<QtSql/QSql>
    #include<QtSql/QSqlDatabase>
    #include<QtSql/QSqlQuery>
    #include<QtSql/QSqlDriver>
    #include<QtSql/QSqlQuery>
    #include<QtSql/QSqlQueryModel>
    #include<QSqlError>
    #include<QSqlResult>
    
    // this is for sql files
    
    #endif
    

    and other files are next1 and next2

    #ifndef NEXT1
    #define NEXT1
    #include<second.h>
    
    
    /// all the other codes
    
    #endif
    
    #ifndef NEXT2
    #define NEXT2
    #include<second.h>
    
    // all the other codes
    
    #endif
    

    These are all the files. Now SIM class has public variable. I want to use the same thing in all included files. I want simple.h to be used only once in the whole program. So that it doesn't fetch data every single time it is used. If you didn't understand this , We have used second.h in next1 and next2. For the both files it re fetches the data from file which isn't necessary. So how can I make it so that it's only one instance is used all over the thing. I know about static but I am not sure how to use this in this place. Are there other methods to do this.

    I hope it's OK to talk about this topic here

    Uff! the question becomes very long


  • Lifetime Qt Champion

    @Thank-You said in Headers in c++:

    I want simple.h to be used only once in the whole program

    Please clarify: do you want to have only one INSTANCE of the class defined in that header file? Or do you mean something else?



  • @jsulm
    I want to be included only once in the whole program. So Yes I want it to have only one instance of the class in all other files


  • Lifetime Qt Champion

    @Thank-You said in Headers in c++:

    I want to be included only once in the whole program. So Yes I want it to have only one instance of the class in all other files

    These are actually two different things.
    If you want to make sure you only have one instance of a class in whole application make it https://en.wikipedia.org/wiki/Singleton_pattern It doesn't matter then how often you include the header file. You anyway have to include it if you want to access the instance of that class.



  • @jsulm
    The main purpose of asking this is to fetch something like "database name" , "and other details" which are to be fetched only at the beginning of the program. Because they won't be changed for different implementations


  • Moderators

    @Thank-You said in Headers in c++:

    The main purpose of asking this is to fetch something like "database name" , "and other details" which are to be fetched only at the beginning of the program. Because they won't be changed for different implementations

    in that case,
    why don't you fetch those information in the beginning, before you instantiate the other classes. and pass those information in the constructor as a const reference?


  • Lifetime Qt Champion

    Or simply use a proper Configuration class (which internally uses QSettings for example) where you can retrieve all those information from?


Log in to reply