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. how can i pass my array object in function arguments by reference?

how can i pass my array object in function arguments by reference?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestiondata-structureshelp
15 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.
  • A Offline
    A Offline
    amitmistry_petlad
    wrote on last edited by
    #1

    hi ! i need to pass array of objects in the function by reference. can any one help me ? i created the following class declaration. class TagE { string x, string y, int z, public : void foo(TagE & )//?:confused: :confused: what should i write here? }; int main() { TagE t[25]; // how can i pass t[25] as function argument foo(t);// what should i write here } :confused: can any guide me or send me test/demo/example link? Amit

    N E M 3 Replies Last reply
    0
    • A amitmistry_petlad

      hi ! i need to pass array of objects in the function by reference. can any one help me ? i created the following class declaration. class TagE { string x, string y, int z, public : void foo(TagE & )//?:confused: :confused: what should i write here? }; int main() { TagE t[25]; // how can i pass t[25] as function argument foo(t);// what should i write here } :confused: can any guide me or send me test/demo/example link? Amit

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      amitmistry_petlad wrote:

      void foo(TagE & )//

      u can pass the array to foo by changing the prototype of foo as below void foo(TagE* );

      nave

      A 1 Reply Last reply
      0
      • N Naveen

        amitmistry_petlad wrote:

        void foo(TagE & )//

        u can pass the array to foo by changing the prototype of foo as below void foo(TagE* );

        nave

        A Offline
        A Offline
        amitmistry_petlad
        wrote on last edited by
        #3

        but i want to use reference rather then pointer.

        N 1 Reply Last reply
        0
        • A amitmistry_petlad

          but i want to use reference rather then pointer.

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          no reference is needed when passing array when you call as foo( t ), the t is the pointer to the first element to the array so the foo will get the pointer to the original array it self. Its not getting copied. or else u can define foo as foo( T array[]);

          nave

          1 Reply Last reply
          0
          • A amitmistry_petlad

            hi ! i need to pass array of objects in the function by reference. can any one help me ? i created the following class declaration. class TagE { string x, string y, int z, public : void foo(TagE & )//?:confused: :confused: what should i write here? }; int main() { TagE t[25]; // how can i pass t[25] as function argument foo(t);// what should i write here } :confused: can any guide me or send me test/demo/example link? Amit

            E Offline
            E Offline
            Eytukan
            wrote on last edited by
            #5
            #include <vector>
            using namespace std;
            class TagE
            {
            public :
            int z;
            string x;
            string y;
            void foo(vector<TagE>& );
            };
            
            void TagE::foo(vector<TagE>& vec_tag)
            {
            	cout<
            

            * * * **Dario:** How is "directory" in French? (I mean a file system directory). **John Simmons:** "zee file holdaire thingie"

            1 Reply Last reply
            0
            • A amitmistry_petlad

              hi ! i need to pass array of objects in the function by reference. can any one help me ? i created the following class declaration. class TagE { string x, string y, int z, public : void foo(TagE & )//?:confused: :confused: what should i write here? }; int main() { TagE t[25]; // how can i pass t[25] as function argument foo(t);// what should i write here } :confused: can any guide me or send me test/demo/example link? Amit

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              I think it would be:

              void foo(TagE (&the_array)[25]);

              although it would be clearer with a typedef:

              typedef TagE array_of_25_tage[25];
              void foo(array_of_25_tage& the_array);

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

              N A 2 Replies Last reply
              0
              • M Michael Dunn

                I think it would be:

                void foo(TagE (&the_array)[25]);

                although it would be clearer with a typedef:

                typedef TagE array_of_25_tage[25];
                void foo(array_of_25_tage& the_array);

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #7

                Michael Dunn wrote:

                void foo(TagE (&the_array)[25]);

                is there any difference between the above and void foo(TagE the_array[25]);?

                nave

                M 1 Reply Last reply
                0
                • M Michael Dunn

                  I think it would be:

                  void foo(TagE (&the_array)[25]);

                  although it would be clearer with a typedef:

                  typedef TagE array_of_25_tage[25];
                  void foo(array_of_25_tage& the_array);

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                  A Offline
                  A Offline
                  amitmistry_petlad
                  wrote on last edited by
                  #8

                  i exactly definded you what i want . in my DRmClient.cpp file #include "TagEditor.h" //// for tageditor class. TagEditor _ProtectSet[MAX_PATH]; //global object //// now i had fill up the object run time and try to pass this object to one function below hr = pkg.EncodeMediaContent(wInFile,w_Output,hDlg,HOST,UserID,sINIT_PACKAGE_RESPONSE,COMMUNICATOR_SCRIPT_FILE_PATH,PORT,hList,_ProtectSet[25]); the above function call the follwoing function HRESULT Package::EncodeMediaContent(WCHAR *pszInFile, WCHAR *pszOutFile, HWND hwndParent, string Host,string UserID, string InitPackageRequest,string ScriptFile,INTERNET_PORT Port,HWND hList,TagEditor _ProtectSet[MAX_PATH]) { //where i again pass that last parameter in other function string sConfigureResponse = GenerateConfigurePackageRequest(UserID,Host,ScriptFile,Port,hwndParent,InitPackageRequest,w_KeyID,hList,_ProtectSet); } string Package::GenerateConfigurePackageRequest(std::string uid,std::string host, std::string scriptFile, INTERNET_PORT port, HWND hwndParent,string sInitPackagingResponse,WCHAR *w_KeyID,HWND hList,TagEditor _ProtectSet[]) { //and here i am using the original object members e.g. like string Title = ""; Title=_ProtectSet[0].title; // now got it!!. } } Amit

                  N 1 Reply Last reply
                  0
                  • A amitmistry_petlad

                    i exactly definded you what i want . in my DRmClient.cpp file #include "TagEditor.h" //// for tageditor class. TagEditor _ProtectSet[MAX_PATH]; //global object //// now i had fill up the object run time and try to pass this object to one function below hr = pkg.EncodeMediaContent(wInFile,w_Output,hDlg,HOST,UserID,sINIT_PACKAGE_RESPONSE,COMMUNICATOR_SCRIPT_FILE_PATH,PORT,hList,_ProtectSet[25]); the above function call the follwoing function HRESULT Package::EncodeMediaContent(WCHAR *pszInFile, WCHAR *pszOutFile, HWND hwndParent, string Host,string UserID, string InitPackageRequest,string ScriptFile,INTERNET_PORT Port,HWND hList,TagEditor _ProtectSet[MAX_PATH]) { //where i again pass that last parameter in other function string sConfigureResponse = GenerateConfigurePackageRequest(UserID,Host,ScriptFile,Port,hwndParent,InitPackageRequest,w_KeyID,hList,_ProtectSet); } string Package::GenerateConfigurePackageRequest(std::string uid,std::string host, std::string scriptFile, INTERNET_PORT port, HWND hwndParent,string sInitPackagingResponse,WCHAR *w_KeyID,HWND hList,TagEditor _ProtectSet[]) { //and here i am using the original object members e.g. like string Title = ""; Title=_ProtectSet[0].title; // now got it!!. } } Amit

                    N Offline
                    N Offline
                    Naveen
                    wrote on last edited by
                    #9

                    amitmistry_petlad wrote:

                    hr = pkg.EncodeMediaContent(wInFile,w_Output,hDlg,HOST,UserID,sINIT_PACKAGE_RESPONSE,COMMUNICATOR_SCRIPT_FILE_PATH,PORT,hList,_ProtectSet[25]);

                    why _ProtectSet[25]? Passing like this means you are passing only the 25th element of the array. Why dont you pass like hr = pkg.EncodeMediaContent(wInFile,w_Output,hDlg,HOST,UserID,sINIT_PACKAGE_RESPONSE,COMMUNICATOR_SCRIPT_FILE_PATH,PORT,hList,_ProtectSet );

                    nave

                    A 1 Reply Last reply
                    0
                    • N Naveen

                      amitmistry_petlad wrote:

                      hr = pkg.EncodeMediaContent(wInFile,w_Output,hDlg,HOST,UserID,sINIT_PACKAGE_RESPONSE,COMMUNICATOR_SCRIPT_FILE_PATH,PORT,hList,_ProtectSet[25]);

                      why _ProtectSet[25]? Passing like this means you are passing only the 25th element of the array. Why dont you pass like hr = pkg.EncodeMediaContent(wInFile,w_Output,hDlg,HOST,UserID,sINIT_PACKAGE_RESPONSE,COMMUNICATOR_SCRIPT_FILE_PATH,PORT,hList,_ProtectSet );

                      nave

                      A Offline
                      A Offline
                      amitmistry_petlad
                      wrote on last edited by
                      #10

                      ok fine suppose i will pass like that then i should write pointer at recever end is it ?. then how can i access the members. because i passing this array object through two function 1) EncodeMediaContent(......,last param) //here i should put pointer 2) GenerateConfigurePackageRequest(..., last param )//here i should pointer Both last param having the same. so now you can give me the guide line. i have take pointer at two place but let me know how can i got the objects value. e.g. Title=_ProtectSet[0].title; Amit

                      N 1 Reply Last reply
                      0
                      • A amitmistry_petlad

                        ok fine suppose i will pass like that then i should write pointer at recever end is it ?. then how can i access the members. because i passing this array object through two function 1) EncodeMediaContent(......,last param) //here i should put pointer 2) GenerateConfigurePackageRequest(..., last param )//here i should pointer Both last param having the same. so now you can give me the guide line. i have take pointer at two place but let me know how can i got the objects value. e.g. Title=_ProtectSet[0].title; Amit

                        N Offline
                        N Offline
                        Naveen
                        wrote on last edited by
                        #11

                        amitmistry_petlad wrote:

                        1. EncodeMediaContent(......,last param) //here i should put pointer 2) GenerateConfigurePackageRequest(..., last param )//here i should pointer

                        you dont need to change this parameters. The current definition is ok. And even though u recieve a pointer in the last function, you will be able to use like Title=_ProtectSet[0].title; Modify the code as I said and try compliling the code. It should compile with zero errors.And let me know if there is any error.

                        nave

                        A 1 Reply Last reply
                        0
                        • N Naveen

                          amitmistry_petlad wrote:

                          1. EncodeMediaContent(......,last param) //here i should put pointer 2) GenerateConfigurePackageRequest(..., last param )//here i should pointer

                          you dont need to change this parameters. The current definition is ok. And even though u recieve a pointer in the last function, you will be able to use like Title=_ProtectSet[0].title; Modify the code as I said and try compliling the code. It should compile with zero errors.And let me know if there is any error.

                          nave

                          A Offline
                          A Offline
                          amitmistry_petlad
                          wrote on last edited by
                          #12

                          ok done

                          1 Reply Last reply
                          0
                          • N Naveen

                            Michael Dunn wrote:

                            void foo(TagE (&the_array)[25]);

                            is there any difference between the above and void foo(TagE the_array[25]);?

                            nave

                            M Offline
                            M Offline
                            Michael Dunn
                            wrote on last edited by
                            #13

                            Consider the case where you assign a new value to the_array inside foo(). When the_array is a reference, the change is visible to the caller. (I'm not positive, this is an area of black-magick syntax that I rarely venture into)

                            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                            N 1 Reply Last reply
                            0
                            • M Michael Dunn

                              Consider the case where you assign a new value to the_array inside foo(). When the_array is a reference, the change is visible to the caller. (I'm not positive, this is an area of black-magick syntax that I rarely venture into)

                              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                              N Offline
                              N Offline
                              Naveen
                              wrote on last edited by
                              #14

                              Ya in that case its ok. Like a double pointer na? Thanks

                              nave

                              H 1 Reply Last reply
                              0
                              • N Naveen

                                Ya in that case its ok. Like a double pointer na? Thanks

                                nave

                                H Offline
                                H Offline
                                hsuch
                                wrote on last edited by
                                #15

                                TagE the_array[20];
                                void foo(TagE the_array[25]);?
                                foo(the_array); //OK

                                In this case, you can pass a pointer to the function.

                                TagE the_array[20];
                                void foo(TagE (&the_array)[25]);

                                foo(the_array); //Error

                                But in this case, you should pass a correct arrary to the function(TagE the_array[25];).

                                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