Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Headers in c++

Headers in c++

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 4 Posters 841 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Thank YouT Offline
    Thank YouT Offline
    Thank You
    wrote on last edited by
    #1

    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

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    jsulmJ 1 Reply Last reply
    0
    • Thank YouT Thank You

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Thank YouT 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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?

        Thank YouT Offline
        Thank YouT Offline
        Thank You
        wrote on last edited by
        #3

        @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

        Let's make QT free or It will go forever

        TRUE AND FALSE <3

        jsulmJ 1 Reply Last reply
        0
        • Thank YouT Thank You

          @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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Thank YouT 1 Reply Last reply
          2
          • jsulmJ jsulm

            @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.

            Thank YouT Offline
            Thank YouT Offline
            Thank You
            wrote on last edited by
            #5

            @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

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            J.HilkJ 1 Reply Last reply
            0
            • Thank YouT Thank You

              @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

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              Christian EhrlicherC 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @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?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved