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
Forum Updated to NodeBB v4.3 + New Features

Boolean variable - One time declaration - definition

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 6 Posters 7.0k 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.
  • KonstantinosK Offline
    KonstantinosK Offline
    Konstantinos
    wrote on last edited by Konstantinos
    #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.

    VRoninV kshegunovK 2 Replies Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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
      • KonstantinosK Konstantinos

        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.

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on 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
        • KonstantinosK Konstantinos

          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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on 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

            jsulmJ 1 Reply Last reply
            0
            • Pradeep KumarP Pradeep Kumar

              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,

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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

              Pradeep KumarP 1 Reply Last reply
              0
              • jsulmJ jsulm

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

                Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on 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