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. initialization of member variables

initialization of member variables

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 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.
  • K Offline
    K Offline
    Kranti1251984
    wrote on last edited by
    #1

    hi, i've 2 classes ....... class1 { public: CString str1; get(); put(); } class2 { public: CString str2; class2(CString str) { str2 = str; } } when i'm calling constructor of class2 in put function of class1 str2 gets set ... but when i access str2 in a function of class2, it is blank .... can't we use a constructor to set values of members in this way? Thanks, Kranti

    K V 2 Replies Last reply
    0
    • K Kranti1251984

      hi, i've 2 classes ....... class1 { public: CString str1; get(); put(); } class2 { public: CString str2; class2(CString str) { str2 = str; } } when i'm calling constructor of class2 in put function of class1 str2 gets set ... but when i access str2 in a function of class2, it is blank .... can't we use a constructor to set values of members in this way? Thanks, Kranti

      K Offline
      K Offline
      khan
      wrote on last edited by
      #2

      What you are actually doing is to construct an object of class2 in class1. class1's object is initializing the class2's objects's variable to a certain value. But any more objects of class2 which you create outside will not be affected by that. Unless you are using a hierarchy, like class1 is derived from class2. So now you could create an object of class1, and it will contain all non-private members of class2 as well. this is this.

      1 Reply Last reply
      0
      • K Kranti1251984

        hi, i've 2 classes ....... class1 { public: CString str1; get(); put(); } class2 { public: CString str2; class2(CString str) { str2 = str; } } when i'm calling constructor of class2 in put function of class1 str2 gets set ... but when i access str2 in a function of class2, it is blank .... can't we use a constructor to set values of members in this way? Thanks, Kranti

        V Offline
        V Offline
        Vinaya
        wrote on last edited by
        #3

        Kranti1251984 wrote:

        but when i access str2 in a function of class2, it is blank ....

        Where is the object of class2 created? Are you using the same class2 object to access the member function? Vini

        K 1 Reply Last reply
        0
        • V Vinaya

          Kranti1251984 wrote:

          but when i access str2 in a function of class2, it is blank ....

          Where is the object of class2 created? Are you using the same class2 object to access the member function? Vini

          K Offline
          K Offline
          Kranti1251984
          wrote on last edited by
          #4

          object of class1 is being created inside a function of class2 to set the value of a member of class1 thru' a parameterized constructor of class2 as class1 { public: CString str1; get(); { //get str1 } put() { class2 c2(str1); } } class2 { public: CString str2; class2(CString str) { str2 = str; } } Thanks, Kranti

          V S 2 Replies Last reply
          0
          • K Kranti1251984

            object of class1 is being created inside a function of class2 to set the value of a member of class1 thru' a parameterized constructor of class2 as class1 { public: CString str1; get(); { //get str1 } put() { class2 c2(str1); } } class2 { public: CString str2; class2(CString str) { str2 = str; } } Thanks, Kranti

            V Offline
            V Offline
            Vinaya
            wrote on last edited by
            #5

            Kranti1251984 wrote:

            put() { class2 c2(str1); }

            If you are using the same c2 object to access the member function, you will get the value of the variable str2. The object c2 is destroyed once you exit the put() function of class1. Vini

            S 1 Reply Last reply
            0
            • K Kranti1251984

              object of class1 is being created inside a function of class2 to set the value of a member of class1 thru' a parameterized constructor of class2 as class1 { public: CString str1; get(); { //get str1 } put() { class2 c2(str1); } } class2 { public: CString str2; class2(CString str) { str2 = str; } } Thanks, Kranti

              S Offline
              S Offline
              Salad Bar
              wrote on last edited by
              #6

              Within class1::put, c2.str2 will be equal to str1, what is the problem again?

              K 1 Reply Last reply
              0
              • S Salad Bar

                Within class1::put, c2.str2 will be equal to str1, what is the problem again?

                K Offline
                K Offline
                Kranti1251984
                wrote on last edited by
                #7

                i wanted to use str2 thruough out. It's ok now, solved it by using a member function to set the value in class1. Thank you all! Kranti

                1 Reply Last reply
                0
                • V Vinaya

                  Kranti1251984 wrote:

                  put() { class2 c2(str1); }

                  If you are using the same c2 object to access the member function, you will get the value of the variable str2. The object c2 is destroyed once you exit the put() function of class1. Vini

                  S Offline
                  S Offline
                  sunit5
                  wrote on last edited by
                  #8

                  In ur case lifetime of the object of class2 is within the class1::put().To check this u can define the destructor of class2 and check it never say die

                  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