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. Boolean variable - One time declaration - definition
QtWS25 Last Chance

Boolean variable - One time declaration - definition

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 6 Posters 6.9k 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.
  • K Offline
    K Offline
    Konstantinos
    wrote on 4 Jul 2017, 14:19 last edited by Konstantinos 7 Apr 2017, 14:20
    #1

    Hi all. I want to have a Boolean variable, which will be declared and defined, only one time.

    The global variable with the "extern", is declared one time, but it should be defined many times in any class that is used.

    So my question is this: Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?

    Thanks in advance.

    V K 2 Replies Last reply 4 Jul 2017, 16:25
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 4 Jul 2017, 14:29 last edited by mrjj 7 Apr 2017, 14:30
      #2

      Hi
      Im not sure what you are asking.

      Say we have
      myvars.h
      extern bool globalbool;

      and in myvars.cpp
      #include "myvars.h"
      bool globalbool;

      Then in
      other.cpp file
      #include "myvars.h"

      and you can use globalbool directly.

      Note: this is not nice code/ good design.

      Can I ask what the goal is ?

      (moved to c++)

      1 Reply Last reply
      5
      • K Konstantinos
        4 Jul 2017, 14:19

        Hi all. I want to have a Boolean variable, which will be declared and defined, only one time.

        The global variable with the "extern", is declared one time, but it should be defined many times in any class that is used.

        So my question is this: Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?

        Thanks in advance.

        V Offline
        V Offline
        VRonin
        wrote on 4 Jul 2017, 16:25 last edited by
        #3

        @Konstantinos said in Boolean variable - One time declaration - definition:

        Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?

        yes, a that's a global static.

        just create a header global.h and type bool myGlobalBool = true; in it. now just `#inlcude "global.h" to use it.

        An important warning:

        if you need such a thing your design is probably wrong

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • K Konstantinos
          4 Jul 2017, 14:19

          Hi all. I want to have a Boolean variable, which will be declared and defined, only one time.

          The global variable with the "extern", is declared one time, but it should be defined many times in any class that is used.

          So my question is this: Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?

          Thanks in advance.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 4 Jul 2017, 20:54 last edited by
          #4

          @Konstantinos

          @VRonin said in Boolean variable - One time declaration - definition:

          just create a header global.h and type bool myGlobalBool = true; in it. now just `#inlcude "global.h" to use it.

          Nope, follow @mrjj's advice. Defining a variable in a header is going to give you infinite amount of "symbol redefinition" errors. Declare the variable as extern, define it in a .cpp file. I agree, though, that if you need to do this you're probably doing something suspicious.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • P Offline
            P Offline
            Pradeep Kumar
            wrote on 24 Jul 2017, 11:17 last edited by Pradeep Kumar
            #5

            Hi,

            If u want to use boolean value which is declared and defined, which has to be used across all other class, y dont u use definition variables.

            1. Ex :
              bool value = true;

            u have declared and defined value as true, which will be required for all classes.

            instead use

            1. If ur not changing the value dynamically , go for option definition variable.

            Create the separate class and define

            #define boolValue false
            Or
            #define boolValue true

            So in this u can use boolValue variable in all classes.

            MyFirstClass.cpp

            #include "common.h"
            use the variable boolValue in MyFirstClass

            MySecondClass.cpp

            #include "common.h"
            use the variable boolValue in MySecondClass

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            J 1 Reply Last reply 24 Jul 2017, 12:43
            0
            • P Pradeep Kumar
              24 Jul 2017, 11:17

              Hi,

              If u want to use boolean value which is declared and defined, which has to be used across all other class, y dont u use definition variables.

              1. Ex :
                bool value = true;

              u have declared and defined value as true, which will be required for all classes.

              instead use

              1. If ur not changing the value dynamically , go for option definition variable.

              Create the separate class and define

              #define boolValue false
              Or
              #define boolValue true

              So in this u can use boolValue variable in all classes.

              MyFirstClass.cpp

              #include "common.h"
              use the variable boolValue in MyFirstClass

              MySecondClass.cpp

              #include "common.h"
              use the variable boolValue in MySecondClass

              Thanks,

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 24 Jul 2017, 12:43 last edited by
              #6

              @Pradeep-Kumar said in Boolean variable - One time declaration - definition:

              #define boolValue false

              this does not define a variable, it defines another name for false.
              Whenever you then write boolValue the preprocessor will replace it with false.
              So you can just use false.
              This does not answer the original question.

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

              P 1 Reply Last reply 24 Jul 2017, 12:59
              0
              • J jsulm
                24 Jul 2017, 12:43

                @Pradeep-Kumar said in Boolean variable - One time declaration - definition:

                #define boolValue false

                this does not define a variable, it defines another name for false.
                Whenever you then write boolValue the preprocessor will replace it with false.
                So you can just use false.
                This does not answer the original question.

                P Offline
                P Offline
                Pradeep Kumar
                wrote on 24 Jul 2017, 12:59 last edited by Pradeep Kumar
                #7

                @jsulm

                Hey sorry about the misconception of the topic and my post.

                i should have rephrased saying definition variables can be across all the classes.
                and not as mentioned in the topic as declared and defined.

                Thanks,

                Pradeep Kumar
                Qt,QML Developer

                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