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 Offline
    T Offline
    T RATHA KRISHNAN
    wrote on last edited by
    #1

    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?

    C CPalliniC L 3 Replies Last reply
    0
    • 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?

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      What do you mean by "it prints nothing"? Show some relevant code, don't forget the <pre> </pre> tags please.

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

      1 Reply Last reply
      0
      • 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?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        T.RATHA KRISHNAN wrote:

        I've printed the same variable inside another derived class. But it prints nothing.

        ? :confused: this sample code

        #include <iostream>
        class A
        {
        protected:
        int a;
        public:
        A(int a):a(a){}
        void show(){ std::cout << "this is A, a=" << a << std::endl;}
        };

        class B: public A
        {

        public:
        B(int b):A(b){}
        void show(){ std::cout << "this is B, a=" << a << std::endl;}
        };

        class C: public B
        {

        public:
        C(int c):B(c){}
        void show(){ std::cout << "this is C, a=" << a << std::endl;}
        };

        int main()
        {
        A a(3);
        B b(4);
        C c(5);

        a.show();
        b.show();
        c.show();

        return 0;
        }

        produces:

        this is A, a=3 this is B, a=4 this is C, a=5

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • 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