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. access class members from a non class global static function

access class members from a non class global static function

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

    I have a class A{ public :CString m_str;} static int func()not in the class. How can I acess m_str in this function func()

    X 1 Reply Last reply
    0
    • S SPGV

      I have a class A{ public :CString m_str;} static int func()not in the class. How can I acess m_str in this function func()

      X Offline
      X Offline
      xxhimanshu
      wrote on last edited by
      #2

      make an object of this class like A b; and use it like b.m_str; or make get set functions in class..and then use those functions..but first include this file into the file where your function is if they are in separate files:)..i hope this works.. cheers Himanshu

      S 1 Reply Last reply
      0
      • X xxhimanshu

        make an object of this class like A b; and use it like b.m_str; or make get set functions in class..and then use those functions..but first include this file into the file where your function is if they are in separate files:)..i hope this works.. cheers Himanshu

        S Offline
        S Offline
        SPGV
        wrote on last edited by
        #3

        The case is as below class A {public cstring mstr; func();} static int CompareModes(const void *arg1, const void *arg2 ); A::func() { .... qsort ((void *)lpModes, (size_t)cModes, sizeof(LPDDModeInfo), CompareModes); } int CompareModes(const void *arg1, const void *arg2) { CB *p1, *p2; p1 = *(CB**)arg1; p2 = *(CB**)arg2; CString str1, str2; str1 = p1->GetValue(mstr); str2 = p2->GetValue(mstr); return str1.CompareNoCase(str2); } here I need to access mstr in the static func CompareModes

        X 1 Reply Last reply
        0
        • S SPGV

          The case is as below class A {public cstring mstr; func();} static int CompareModes(const void *arg1, const void *arg2 ); A::func() { .... qsort ((void *)lpModes, (size_t)cModes, sizeof(LPDDModeInfo), CompareModes); } int CompareModes(const void *arg1, const void *arg2) { CB *p1, *p2; p1 = *(CB**)arg1; p2 = *(CB**)arg2; CString str1, str2; str1 = p1->GetValue(mstr); str2 = p2->GetValue(mstr); return str1.CompareNoCase(str2); } here I need to access mstr in the static func CompareModes

          X Offline
          X Offline
          xxhimanshu
          wrote on last edited by
          #4

          :)hi, thats what i was telling you..but i guess you didn't understood..anyway..inside your function like this int CompareModes(const void *arg1, const void *arg2) { [red] a b; CB *p1, *p2; p1 = *(CB**)arg1; p2 = *(CB**)arg2; CString str1, str2; //in place of mstr use b.mstr and that will do.. str1 = p1->GetValue(mstr); str2 = p2->GetValue(mstr); return str1.CompareNoCase(str2); } i hope you r done this time.. cheers Himanshu

          S 1 Reply Last reply
          0
          • X xxhimanshu

            :)hi, thats what i was telling you..but i guess you didn't understood..anyway..inside your function like this int CompareModes(const void *arg1, const void *arg2) { [red] a b; CB *p1, *p2; p1 = *(CB**)arg1; p2 = *(CB**)arg2; CString str1, str2; //in place of mstr use b.mstr and that will do.. str1 = p1->GetValue(mstr); str2 = p2->GetValue(mstr); return str1.CompareNoCase(str2); } i hope you r done this time.. cheers Himanshu

            S Offline
            S Offline
            SPGV
            wrote on last edited by
            #5

            I guess more info had to be given, A is a dlg class and I have a combo in it and onSelandOK I fill the mStr member. The on clicking another button I do some sorting and there in I need the mstr. So there is no point in creating a new obj in comparemodes. I need to access the member's value of the same obj that's calling func()

            R X 2 Replies Last reply
            0
            • S SPGV

              I guess more info had to be given, A is a dlg class and I have a combo in it and onSelandOK I fill the mStr member. The on clicking another button I do some sorting and there in I need the mstr. So there is no point in creating a new obj in comparemodes. I need to access the member's value of the same obj that's calling func()

              R Offline
              R Offline
              Rage
              wrote on last edited by
              #6

              You should not retrieve the string value in CompareModes, but in your dialog. Extend your comparison function to pass the strings retrieved in parameter. :cool: Crap, you cannot since CompareModes is a callback. Why is your pointer cast not working ? Does it compile ? If yes, do you have an error at runtime ? ~RaGE();

              1 Reply Last reply
              0
              • S SPGV

                I guess more info had to be given, A is a dlg class and I have a combo in it and onSelandOK I fill the mStr member. The on clicking another button I do some sorting and there in I need the mstr. So there is no point in creating a new obj in comparemodes. I need to access the member's value of the same obj that's calling func()

                X Offline
                X Offline
                xxhimanshu
                wrote on last edited by
                #7

                that's what was my secong idea..use get and set functions like.. void setmstr(cstring _mstr){mstr=_mstr;} CString Getmstr(){ return mstr;} so where you click to set mstr call setmstr() and it will store the value for you and where you need the value call getmstr() function which returns a CString.. so i guess that will do.. PS don't unfold the problem one by one..its gud to understand each and everything once.. Himanshu

                S 1 Reply Last reply
                0
                • X xxhimanshu

                  that's what was my secong idea..use get and set functions like.. void setmstr(cstring _mstr){mstr=_mstr;} CString Getmstr(){ return mstr;} so where you click to set mstr call setmstr() and it will store the value for you and where you need the value call getmstr() function which returns a CString.. so i guess that will do.. PS don't unfold the problem one by one..its gud to understand each and everything once.. Himanshu

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

                  well I guess Get/Set are member func if not I wonder how Get will return mstr??. If they are member funcs then back to square one!!! I did not know that I could access member funcs in global static function(CompareModes) then I would not have faced any problem :-(

                  R 1 Reply Last reply
                  0
                  • S SPGV

                    well I guess Get/Set are member func if not I wonder how Get will return mstr??. If they are member funcs then back to square one!!! I did not know that I could access member funcs in global static function(CompareModes) then I would not have faced any problem :-(

                    R Offline
                    R Offline
                    Rage
                    wrote on last edited by
                    #9

                    Actually, what is your compare function comparing ? if you passed the dialogs as arguments, (I means arg1 and arg2 are dialogs), then your pointer cast should do : CDialogA *pDlgA = (CDialog A*) arg1; // I'm not sure if i'm not missing a * here ... and you should be able to access your data with p1->GetBlaBlaBla(); ~RaGE();

                    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