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. Passing parameters to constructor - very obvious and basic question from an OF

Passing parameters to constructor - very obvious and basic question from an OF

Scheduled Pinned Locked Moved C / C++ / MFC
c++hardwarehelpquestion
5 Posts 4 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

    I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)

    CPalliniC L D 3 Replies Last reply
    0
    • V Vaclav_

      I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)

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

      Could you please post sample code?

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • V Vaclav_

        I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)

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

        It is normal practice to add parameters that remain constant in the object to the constructor like:

        class Foo
        {
        private:
        int paramThatDoesNotChangeOften;
        public:
        Foo(int someParam)
        {
        paramThatDoesNotChangeOften = someParam);
        }
        };

        You could then add a getter method to return the value to the user if it's value is needed outside the class. Is that what you are querying?

        V 1 Reply Last reply
        0
        • L Lost User

          It is normal practice to add parameters that remain constant in the object to the constructor like:

          class Foo
          {
          private:
          int paramThatDoesNotChangeOften;
          public:
          Foo(int someParam)
          {
          paramThatDoesNotChangeOften = someParam);
          }
          };

          You could then add a getter method to return the value to the user if it's value is needed outside the class. Is that what you are querying?

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

          Thanks for replies. It may be stupid, but that is what I am trying to avoid. Now I would have "someParam" - actually global array and a class copy of the same so it can be accessed by class methods. I am trying to save some memory of embedded processor. Kinda of wishful thinking ahead. I won't get to upset if I need to make a local copies. Would using "this" pointer work making local copy? I just tryied "standard" this.name = name but could not make it work on pointers. I guess I am still struggling with using pointers. Addendum I have decided to KISS and I'll be making copies of the parameters using "this" pointer. One question remains - since all of the parameaters passed are de facto constants - harwdware pins, LCD size etc. and won't be modified by the class, why can't I just use "this>LCD_x_size" as variable instead of "this->LCD_x_size = LCD_x_size" Now the class local varaible is "LCD_x_size". And yes, I read somewhere that using same symbols for "name" and "variable" is OK. Confusing, but OK.

          1 Reply Last reply
          0
          • V Vaclav_

            I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)

            D Offline
            D Offline
            Daniel Pfeffer
            wrote on last edited by
            #5

            If the parameters are actually constants, how about instantiating a template class?

            int const PROCESSOR_COUNT = 42;

            template scheduler
            {
            // constructors, other methods

            private:
            processor_data data[processor_count];
            };

            scheduler sched;

            If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

            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