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. constant variable at run time

constant variable at run time

Scheduled Pinned Locked Moved C / C++ / MFC
11 Posts 6 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.
  • A Offline
    A Offline
    Ahmed Ismail Mohamed
    wrote on last edited by
    #1

    I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose and i want him to not not chang it again. thank you:)

    ****************** ****************** ** Ahmed Ismail ** ****************** ******************

    A N 2 Replies Last reply
    0
    • A Ahmed Ismail Mohamed

      I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose and i want him to not not chang it again. thank you:)

      ****************** ****************** ** Ahmed Ismail ** ****************** ******************

      A Offline
      A Offline
      Anonymuos
      wrote on last edited by
      #2

      Ahmed Ismail Mohamed wrote:

      I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose and i want him to not not chang it again.

      const is an official C++ qualifier. :~

      A 1 Reply Last reply
      0
      • A Anonymuos

        Ahmed Ismail Mohamed wrote:

        I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose and i want him to not not chang it again.

        const is an official C++ qualifier. :~

        A Offline
        A Offline
        Ahmed Ismail Mohamed
        wrote on last edited by
        #3

        :rolleyes:but i think that a pointer have the address of const var can do changes, but when i make that i failed Ahemd Ismail

        W 1 Reply Last reply
        0
        • A Ahmed Ismail Mohamed

          :rolleyes:but i think that a pointer have the address of const var can do changes, but when i make that i failed Ahemd Ismail

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          If you are declaring a variable as const, and do not want to change it, why on earth are you trying to change it?

          1 Reply Last reply
          0
          • A Ahmed Ismail Mohamed

            I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose and i want him to not not chang it again. thank you:)

            ****************** ****************** ** Ahmed Ismail ** ****************** ******************

            N Offline
            N Offline
            Nader Elshehabi
            wrote on last edited by
            #5

            Hello

            Ahmed Ismail Mohamed wrote:

            I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose

            No problem! Make an input and store in that variable.

            Ahmed Ismail Mohamed wrote:

            and i want him to not not chang it again.

            Don't call the above function again!!:-D PS. I know you are talking about the const_cast operator, but why?? Here is a sample code for what you want quoted from MSDN:

            #include

            using namespace std;
            class CCTest {
            public:
            void setNumber( int );
            void printNumber() const;
            private:
            int number;
            };

            void CCTest::setNumber( int num ) { number = num; }

            void CCTest::printNumber() const {
            cout << "\nBefore: " << number;
            const_cast< CCTest * >( this )->number--;
            cout << "\nAfter: " << number;
            }

            int main() {
            CCTest X;
            X.setNumber( 8 );
            X.printNumber();
            }

            -- modified at 1:16 Sunday 3rd September, 2006 In addition to the above, I forgot to mention another more standard way of doing what you want: 1- Declare your data type as a const member of a class. 2- Take the input from the user in a temp variable. 3- Put the value in your const member using the initialization list of the class' constructor.

            Regards:rose:

            B 1 Reply Last reply
            0
            • N Nader Elshehabi

              Hello

              Ahmed Ismail Mohamed wrote:

              I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose

              No problem! Make an input and store in that variable.

              Ahmed Ismail Mohamed wrote:

              and i want him to not not chang it again.

              Don't call the above function again!!:-D PS. I know you are talking about the const_cast operator, but why?? Here is a sample code for what you want quoted from MSDN:

              #include

              using namespace std;
              class CCTest {
              public:
              void setNumber( int );
              void printNumber() const;
              private:
              int number;
              };

              void CCTest::setNumber( int num ) { number = num; }

              void CCTest::printNumber() const {
              cout << "\nBefore: " << number;
              const_cast< CCTest * >( this )->number--;
              cout << "\nAfter: " << number;
              }

              int main() {
              CCTest X;
              X.setNumber( 8 );
              X.printNumber();
              }

              -- modified at 1:16 Sunday 3rd September, 2006 In addition to the above, I forgot to mention another more standard way of doing what you want: 1- Declare your data type as a const member of a class. 2- Take the input from the user in a temp variable. 3- Put the value in your const member using the initialization list of the class' constructor.

              Regards:rose:

              B Offline
              B Offline
              Bram van Kampen
              wrote on last edited by
              #6

              The Standard of English in the phrasing of this request was so bad, I still do not understand what it was all about. The Code snippet above is quite clear, but I've still no idea what question it was realy supposed to answer. I suggest that the poser of this question purchases a copy of Kernighan & Ritchie, and Read it from Cover to Cover. If nothing else, this will introduce the poser of the question to the English Language

              LateNightsInNewry

              N 1 Reply Last reply
              0
              • B Bram van Kampen

                The Standard of English in the phrasing of this request was so bad, I still do not understand what it was all about. The Code snippet above is quite clear, but I've still no idea what question it was realy supposed to answer. I suggest that the poser of this question purchases a copy of Kernighan & Ritchie, and Read it from Cover to Cover. If nothing else, this will introduce the poser of the question to the English Language

                LateNightsInNewry

                N Offline
                N Offline
                Nader Elshehabi
                wrote on last edited by
                #7

                Hello

                LateNightsInNewry wrote:

                I still do not understand what it was all about

                I believe he was asking about casting a const variable to remove its constancy, change its value once, then recasting it back into a const.

                LateNightsInNewry wrote:

                this will introduce the poser of the question to the English Language

                Come on! You already have spent enough time around here to learn CP language!;)

                Regards:rose:

                M 1 Reply Last reply
                0
                • N Nader Elshehabi

                  Hello

                  LateNightsInNewry wrote:

                  I still do not understand what it was all about

                  I believe he was asking about casting a const variable to remove its constancy, change its value once, then recasting it back into a const.

                  LateNightsInNewry wrote:

                  this will introduce the poser of the question to the English Language

                  Come on! You already have spent enough time around here to learn CP language!;)

                  Regards:rose:

                  M Offline
                  M Offline
                  MayankT
                  wrote on last edited by
                  #8

                  Nader Elshehabi wrote:

                  I believe he was asking about casting a const variable to remove its constancy

                  not so. i believe he wants to know how to initialize a const var whose value will be known only at runtime. your answer though was correct. making the const var a class member can delay init till runtime. but it is expensive. a better way is to put it in a function as a static const and then init it with an incoming value which has a default. like this: const int var_name (const int var_val = 0) { static const int var = var_val; return var; } then you can use it like this: var_name (temp1); // set const value temp3 = var_name (temp2); // no effect of temp2; temp3 == temp1 but beware that if your first call doesn't pass a value the default will be set. one more advantage is that this way it is much more difficult to use a pointer and modify the value. -- modified at 5:46 Sunday 3rd September, 2006

                  ---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.

                  N 1 Reply Last reply
                  0
                  • M MayankT

                    Nader Elshehabi wrote:

                    I believe he was asking about casting a const variable to remove its constancy

                    not so. i believe he wants to know how to initialize a const var whose value will be known only at runtime. your answer though was correct. making the const var a class member can delay init till runtime. but it is expensive. a better way is to put it in a function as a static const and then init it with an incoming value which has a default. like this: const int var_name (const int var_val = 0) { static const int var = var_val; return var; } then you can use it like this: var_name (temp1); // set const value temp3 = var_name (temp2); // no effect of temp2; temp3 == temp1 but beware that if your first call doesn't pass a value the default will be set. one more advantage is that this way it is much more difficult to use a pointer and modify the value. -- modified at 5:46 Sunday 3rd September, 2006

                    ---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.

                    N Offline
                    N Offline
                    Nader Elshehabi
                    wrote on last edited by
                    #9

                    Hello Thank you for your reply Mayank.

                    MayankT wrote:

                    but it is expensive

                    You didn't specify in what exactly, but I'll assume you mean in excution speed. Why? What's the difference between declaring a variable as a class member or as a static in a function in a class?? Also in your way you store the true value in the static member. But from your code most of the work will be on the returned value not the original const. You can change temp3 to whatever you want after the call. While in the initialization list of a constructor -i hope you read my modified post-, you set that memebr to public and operate on it with no problem! Again, about the whole idea!! I can't think of one good practical -not hypothtical- scenario where we should use this approach instead of just making our variable as private, or just use another approach!!

                    MayankT wrote:

                    one more advantage is that this way it is much more difficult to use a pointer and modify the value.

                    I hope you don't mean reverse engineering? From what I heard there are monsters out there that can decipher assembly as easy as you read english! With a cup of coffee, some brains, and a good memory dump tool, you can't hide aything anywhere!!

                    Regards:rose:

                    M 1 Reply Last reply
                    0
                    • N Nader Elshehabi

                      Hello Thank you for your reply Mayank.

                      MayankT wrote:

                      but it is expensive

                      You didn't specify in what exactly, but I'll assume you mean in excution speed. Why? What's the difference between declaring a variable as a class member or as a static in a function in a class?? Also in your way you store the true value in the static member. But from your code most of the work will be on the returned value not the original const. You can change temp3 to whatever you want after the call. While in the initialization list of a constructor -i hope you read my modified post-, you set that memebr to public and operate on it with no problem! Again, about the whole idea!! I can't think of one good practical -not hypothtical- scenario where we should use this approach instead of just making our variable as private, or just use another approach!!

                      MayankT wrote:

                      one more advantage is that this way it is much more difficult to use a pointer and modify the value.

                      I hope you don't mean reverse engineering? From what I heard there are monsters out there that can decipher assembly as easy as you read english! With a cup of coffee, some brains, and a good memory dump tool, you can't hide aything anywhere!!

                      Regards:rose:

                      M Offline
                      M Offline
                      MayankT
                      wrote on last edited by
                      #10

                      Nader Elshehabi wrote:

                      What's the difference between declaring a variable as a class member or as a static in a function in a class??

                      Actually I meant that a class might not be necessary. And if it is an existing class that is being used then it could be difficult to change the design in a manner as to provide the var at object creation time. This function can be put outside a class or if inside it wont require change to construction code. One difference is that the member var will be unique to each object but as static in a function in a class it will have to be shared.

                      Nader Elshehabi wrote:

                      You can change temp3 to whatever you want after the call.

                      Right! I was tempted to return a val by ref but realized that the guy had already tried to use pointers to manipulate a const var. So this seems safer. Also, even a public class member can be assigned to a temp and anything can be done.

                      Nader Elshehabi wrote:

                      scenario where we should use this approach

                      as in the differences above

                      Nader Elshehabi wrote:

                      I hope you don't mean reverse engineering?

                      Not at all :). Only in this case even if someone uses a pointer to the returned val they will not be changing the actual var storing the const. Actually, only the first line was for you :) Yes your approach is excellent if there is an existing class which is being instantiated after the value is known. I should have thought about that. Thanks for the reply!

                      ---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.

                      N 1 Reply Last reply
                      0
                      • M MayankT

                        Nader Elshehabi wrote:

                        What's the difference between declaring a variable as a class member or as a static in a function in a class??

                        Actually I meant that a class might not be necessary. And if it is an existing class that is being used then it could be difficult to change the design in a manner as to provide the var at object creation time. This function can be put outside a class or if inside it wont require change to construction code. One difference is that the member var will be unique to each object but as static in a function in a class it will have to be shared.

                        Nader Elshehabi wrote:

                        You can change temp3 to whatever you want after the call.

                        Right! I was tempted to return a val by ref but realized that the guy had already tried to use pointers to manipulate a const var. So this seems safer. Also, even a public class member can be assigned to a temp and anything can be done.

                        Nader Elshehabi wrote:

                        scenario where we should use this approach

                        as in the differences above

                        Nader Elshehabi wrote:

                        I hope you don't mean reverse engineering?

                        Not at all :). Only in this case even if someone uses a pointer to the returned val they will not be changing the actual var storing the const. Actually, only the first line was for you :) Yes your approach is excellent if there is an existing class which is being instantiated after the value is known. I should have thought about that. Thanks for the reply!

                        ---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.

                        N Offline
                        N Offline
                        Nader Elshehabi
                        wrote on last edited by
                        #11

                        Hello Thanks Mayank for your replies!:) It was really nice to have this talk with you.

                        Regards:rose:

                        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