Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. setting static struct and read
Forum Updated to NodeBB v4.3 + New Features

setting static struct and read

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 2.1k Views 3 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    You just declared the struct itself. There's object of that type anywhere.

    In any case, what are the motivation behind that design ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    elicatE 1 Reply Last reply
    2
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #3

      static variables must be defined.
      example:

      class Base
      {
      public:
      
      struct Inner {
               static int type;
               };
      };
      
      int Base::Inner::type=1;  // defined/initialized here
      
      int main(int argc, char *argv[])
      {
      qDebug()<<Base::Inner::type;  // print 1
      
      1 Reply Last reply
      4
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #4

        As a better alternative i think, you can use a static inner struct:

        class Base
        {
        public:
        
        struct Inner {
                 int type;
                 int val;
                 };
        
        
        static Inner in;  // declared as static 
        
        };
        
        Base::Inner Base::in{1,10}; // define and init the struct
        
        int main(int argc, char *argv[])
        {
        qDebug()<<Base::in.type<<Base::in.val;  // print 1 10
        
        elicatE 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          You just declared the struct itself. There's object of that type anywhere.

          In any case, what are the motivation behind that design ?

          elicatE Offline
          elicatE Offline
          elicat
          wrote on last edited by
          #5

          @SGaist said in setting static struct and read:

          There's object of that type anywhere.

          I need a method for load parameter for my connection db in more position

          Saluti, Gianfranco Elicat

          SGaistS 1 Reply Last reply
          0
          • M mpergand

            As a better alternative i think, you can use a static inner struct:

            class Base
            {
            public:
            
            struct Inner {
                     int type;
                     int val;
                     };
            
            
            static Inner in;  // declared as static 
            
            };
            
            Base::Inner Base::in{1,10}; // define and init the struct
            
            int main(int argc, char *argv[])
            {
            qDebug()<<Base::in.type<<Base::in.val;  // print 1 10
            
            elicatE Offline
            elicatE Offline
            elicat
            wrote on last edited by
            #6

            @mpergand said in setting static struct and read:

            Base::Inner Base::in{1,10}; // define and init the struct

            Hello,
            I have put your simple example into my class with name DichiarativeGlobali.
            I want init the static structure when i call a method settingConnectDb because the value i don't know when i start program.
            Inside the method i have this error:
            0_1535553169832_statistructurexforum.jpg

            Saluti, Gianfranco Elicat

            M 1 Reply Last reply
            0
            • elicatE elicat

              @mpergand said in setting static struct and read:

              Base::Inner Base::in{1,10}; // define and init the struct

              Hello,
              I have put your simple example into my class with name DichiarativeGlobali.
              I want init the static structure when i call a method settingConnectDb because the value i don't know when i start program.
              Inside the method i have this error:
              0_1535553169832_statistructurexforum.jpg

              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #7

              @elicat said in setting static struct and read:

              I want init the static structure when i call a method settingConnectDb

              DichiarativeGlobali::in={1,10};

              elicatE 1 Reply Last reply
              0
              • elicatE elicat

                @SGaist said in setting static struct and read:

                There's object of that type anywhere.

                I need a method for load parameter for my connection db in more position

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @elicat said in setting static struct and read:

                @SGaist said in setting static struct and read:

                There's object of that type anywhere.

                I need a method for load parameter for my connection db in more position

                That doesn't sound good. Why do you need these parameters in more positions ? The QSqlDatabase class already supports retrieving connection informations.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • elicatE Offline
                  elicatE Offline
                  elicat
                  wrote on last edited by
                  #9

                  My project need connect various database, select from user, in more point of program. So i need make more connection. The driver and the position database (local or network) depends on the configuration from the installation.
                  this parameter are setting when start program (argv) and i want use them into method where i connect database.

                  	         static int arg_db_type;
                  		 static QString arg_db_driver;
                  		 static QString arg_user_db;
                  		 static QString arg_pass_db;
                  		 static QString arg_hostname_db;
                  		 static QString arg_port_db;
                  		 static QString arg_connoption_db;
                  
                  

                  In any case, if I understand well the use of static variables (in this case a structure) is good for my training. Do not you think ? :-)

                  Saluti, Gianfranco Elicat

                  1 Reply Last reply
                  0
                  • M mpergand

                    @elicat said in setting static struct and read:

                    I want init the static structure when i call a method settingConnectDb

                    DichiarativeGlobali::in={1,10};

                    elicatE Offline
                    elicatE Offline
                    elicat
                    wrote on last edited by elicat
                    #10

                    @mpergand said in setting static struct and read:

                    DichiarativeGlobali::in={1,10};

                    Hello Said,
                    I had also thought and I had tried but it presents me the following error:
                    0_1535610546689_statistructurexforum.jpg

                    Posto il codice attuale del file h:

                    class DichiarativeGlobali
                    {
                    public:
                    	DichiarativeGlobali();
                    
                    	struct parametriAccessDb {
                    		int arg_db_type;
                    		QString arg_db_driver;
                    		QString arg_user_db;
                    		QString arg_pass_db;
                    		QString arg_hostname_db;
                    		QString arg_port_db;
                    		QString arg_connoption_db;
                    	};
                    	
                    	static struct parametriAccessDb parametriDb;
                    
                        void settingConnectDb(QVector<QString> settingaccessdb);
                    
                    private:	
                    
                    };
                    

                    and this is cpp:

                    DichiarativeGlobali::DichiarativeGlobali()
                    {
                    }
                    void DichiarativeGlobali::settingConnectDb(QVector<QString> settingaccessdb)
                    {
                    	DichiarativeGlobali::parametriDb.arg_db_type = settingaccessdb.value(0).toInt();
                    	DichiarativeGlobali::parametriDb.arg_db_type  = settingaccessdb.value(0).toInt();
                    	DichiarativeGlobali::parametriDb.arg_db_driver = settingaccessdb.value(1);
                    	DichiarativeGlobali::parametriDb.arg_user_db = settingaccessdb.value(2);
                    	DichiarativeGlobali::parametriDb.arg_pass_db = settingaccessdb.value(3);
                    	DichiarativeGlobali::parametriDb.arg_hostname_db = settingaccessdb.value(4);
                    	DichiarativeGlobali::parametriDb.arg_port_db = settingaccessdb.value(5);
                    	DichiarativeGlobali::parametriDb.arg_connoption_db = settingaccessdb.value(6);
                    	
                    }
                    

                    Saluti, Gianfranco Elicat

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #11

                      Seems you have not defined the struct in .cpp, add this at the head:

                      DichiarativeGlobali::parametriAccessDb DichiarativeGlobali::parametriDb;
                      

                      (sorry that's C++ :) )

                      and, since you init this variable in a method of the class, you can simply write:

                      parametriDb.arg_db_type= xxx;
                      

                      Warning:
                      Keep in mine that if you use a static variable (class variable), this variable is unique along all instances of the class,
                      In practice, it means there can only be one user defined in all your prog.
                      Seems ok for now, but If in some future you decide to manage several users, you will fail miserably ;)

                      My advice: use class/static variables with cautions.

                      1 Reply Last reply
                      1

                      • Login

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