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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. academic question - usage of class variable

academic question - usage of class variable

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav

    L CPalliniC D S 4 Replies Last reply
    0
    • V Vaclav_

      This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav

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

      That all depends on what the purpose of the function is, and whether the variable needs to be visible outside the function, and/or outside the class object.

      1 Reply Last reply
      0
      • V Vaclav_

        This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav

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

        Generally speaking, a class variable (a static variable of a class) should not (like an ordinary member variable) directly accessed by the external world, that would go against information hiding. Moreover, you don't need need (and you should not) to pass such variables either to class (static) methods or standard (instance) methods.

        In testa che avete, signor di Ceprano?

        V 1 Reply Last reply
        0
        • CPalliniC CPallini

          Generally speaking, a class variable (a static variable of a class) should not (like an ordinary member variable) directly accessed by the external world, that would go against information hiding. Moreover, you don't need need (and you should not) to pass such variables either to class (static) methods or standard (instance) methods.

          V Offline
          V Offline
          Vaclav_
          wrote on last edited by
          #4

          I probably did not make myself clear in my post. I have a class function / method which manipulates class variable - so how do I make such variable "variable" ? Simple example - if I have need to execute "switch" by varying it. I can either change the class variable or pass the value to a function.

          CPalliniC 1 Reply Last reply
          0
          • V Vaclav_

            I probably did not make myself clear in my post. I have a class function / method which manipulates class variable - so how do I make such variable "variable" ? Simple example - if I have need to execute "switch" by varying it. I can either change the class variable or pass the value to a function.

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

            I don't get you. Maybe I need more caffeine or a code sample.

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • V Vaclav_

              This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Vaclav_ wrote:

              Would assigning it outside the function defeat the "scope" of the class?

              The term "scope" is only meaningful to places where your class object is used (i.e., the owners of class instances). What a class does internally, on its own member variables, is not important to "outsiders."

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              1 Reply Last reply
              0
              • V Vaclav_

                This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                Generally, if you define a member variable of a class, this variable should only be accessed by member functions of that class (or derived class). This is true both for static and non-static member variables. If you need to read or change them from outside the class, define accessor member functions as needed. Any static or non-static function can and may access any member variable. Therefore there is no need to pass it to such a function, unless it is a non-static member variable of a different instance of the class. Example:

                class Vector3i {
                private:
                std::array data;
                public:
                // example 1: change own member variable
                void fill(const int value) {
                data.fill(value);
                }
                // example 2: change member variable from another instance
                void swap(Vector3i& other) {
                std::swap(data, other.data);
                }
                };

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                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