Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Accessing a Variable

Accessing a Variable

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
13 Posts 7 Posters 0 Views 1 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.
  • T T RATHA KRISHNAN

    Hi! I've declared a protected variable in a base class. I've assigned value and print the variable in a derived class. It prints correctly. I've printed the same variable inside another derived class. But it prints nothing. How to print the variable that is altered inside another derived class?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    T.RATHA KRISHNAN wrote:

    I've declared a protected variable in a base class. I've assigned value and print the variable in a derived class. It prints correctly. I've printed the same variable inside another derived class. But it prints nothing. How to print the variable that is altered inside another derived class?

    Are you asking how to share a member variable of base class across different derived classes such that changing in one will reflect in the others?

    ...byte till it megahertz...

    T 1 Reply Last reply
    0
    • L Lost User

      T.RATHA KRISHNAN wrote:

      I've declared a protected variable in a base class. I've assigned value and print the variable in a derived class. It prints correctly. I've printed the same variable inside another derived class. But it prints nothing. How to print the variable that is altered inside another derived class?

      Are you asking how to share a member variable of base class across different derived classes such that changing in one will reflect in the others?

      ...byte till it megahertz...

      T Offline
      T Offline
      T RATHA KRISHNAN
      wrote on last edited by
      #5

      Yes. Exactly what I want.

      A N L 3 Replies Last reply
      0
      • T T RATHA KRISHNAN

        Yes. Exactly what I want.

        A Offline
        A Offline
        AmbiguousName
        wrote on last edited by
        #6

        i think friend functions can be used.... :)

        1 Reply Last reply
        0
        • T T RATHA KRISHNAN

          Yes. Exactly what I want.

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #7

          You want a class variable as opposed to an instance variable. A class variable in C++ is declared using the static keyword.

          class A
          {
          protected:
          static int my_class_variable;
          };

          This variable will be shared among all instances of class A and its derived classes.

          home

          T 1 Reply Last reply
          0
          • T T RATHA KRISHNAN

            Yes. Exactly what I want.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            See what Niklas posted.

            ...byte till it megahertz...

            M 1 Reply Last reply
            0
            • L Lost User

              See what Niklas posted.

              ...byte till it megahertz...

              M Offline
              M Offline
              Moak
              wrote on last edited by
              #9

              Read bleedingfingers' answer. :)

              Chat in Europe :java: Now with 24% more Twitter

              L 1 Reply Last reply
              0
              • N Niklas L

                You want a class variable as opposed to an instance variable. A class variable in C++ is declared using the static keyword.

                class A
                {
                protected:
                static int my_class_variable;
                };

                This variable will be shared among all instances of class A and its derived classes.

                home

                T Offline
                T Offline
                T RATHA KRISHNAN
                wrote on last edited by
                #10

                Hi! If I use static as you said, I got the following linker errors: Error 80 error LNK2001: unresolved external symbol "protected: static int CGameState::musicVolume" (?musicVolume@CGameState@@1HA) D:\Goldminer\GameOptionsMenuState.obj

                L 1 Reply Last reply
                0
                • M Moak

                  Read bleedingfingers' answer. :)

                  Chat in Europe :java: Now with 24% more Twitter

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  hey hey hey don't Moak me I had asked the OP a question and before I could reply after my lunch, Niklas had answered it. I find not following up on a question with a reply to be rude and there was no point in re replying.

                  ...byte till it megahertz...

                  T 1 Reply Last reply
                  0
                  • T T RATHA KRISHNAN

                    Hi! If I use static as you said, I got the following linker errors: Error 80 error LNK2001: unresolved external symbol "protected: static int CGameState::musicVolume" (?musicVolume@CGameState@@1HA) D:\Goldminer\GameOptionsMenuState.obj

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #12

                    You have to later initialize it somewhere, for instance, like

                    int CGameState::musicVolume = 0;

                    ...byte till it megahertz...

                    1 Reply Last reply
                    0
                    • L Lost User

                      hey hey hey don't Moak me I had asked the OP a question and before I could reply after my lunch, Niklas had answered it. I find not following up on a question with a reply to be rude and there was no point in re replying.

                      ...byte till it megahertz...

                      T Offline
                      T Offline
                      T RATHA KRISHNAN
                      wrote on last edited by
                      #13

                      I've answered your question.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups