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. Parameter passsing between classes

Parameter passsing between classes

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

    Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.

    E C J 3 Replies Last reply
    0
    • C chaitannya_m

      Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.

      E Offline
      E Offline
      Electronic75
      wrote on last edited by
      #2

      Maybe you have casting problem. post the code when you call setPackageID from your B class (before taking any action for suicide :))

      C 1 Reply Last reply
      0
      • E Electronic75

        Maybe you have casting problem. post the code when you call setPackageID from your B class (before taking any action for suicide :))

        C Offline
        C Offline
        chaitannya_m
        wrote on last edited by
        #3

        No casting since all the variables are long. I checked in the debug mode that the value is retained in the class as along the variable is in scope, but garbage value is assigned once the value goes beyond the scope. I guess i need to increase the scope of variable to application variable.. How do i do it...?

        S 1 Reply Last reply
        0
        • C chaitannya_m

          Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.

          C Offline
          C Offline
          chaitannya_m
          wrote on last edited by
          #4

          Well i solved the problem time being. by declaring extern long iSelPackageItemID in the .h file and long iSelPackageItemID in .cpp file i got help from here http://www.codeguru.com/forum/showthread.php?threadid=427587 But the core OOP funda remains unanswered, i know this is going beyond the scope but just for knowledge sake. I am also working in java and i haven't seen such kind of syntax, also in a desingning book it stated not to use a global variable since it voilates the basic rules of OOP. So is the above code a good programming practise...?

          1 Reply Last reply
          0
          • C chaitannya_m

            Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.

            J Offline
            J Offline
            JudyL_MD
            wrote on last edited by
            #5

            How are you creating "some value"? The first thing that comes to mind is that it is a variable that has gone out of scope between when you assign it and when you try to reference it. Judy

            C 1 Reply Last reply
            0
            • J JudyL_MD

              How are you creating "some value"? The first thing that comes to mind is that it is a variable that has gone out of scope between when you assign it and when you try to reference it. Judy

              C Offline
              C Offline
              chaitannya_m
              wrote on last edited by
              #6

              I am not clear on this thing ... but now it looks like pretty much that it's going out of reference. My OOP fundas are weak... but just one question when i assign a value to a class member A, then i declare an obj of class B so when i try to access the member from a through B does it go out of scope..?

              J 1 Reply Last reply
              0
              • C chaitannya_m

                No casting since all the variables are long. I checked in the debug mode that the value is retained in the class as along the variable is in scope, but garbage value is assigned once the value goes beyond the scope. I guess i need to increase the scope of variable to application variable.. How do i do it...?

                S Offline
                S Offline
                sheshidar
                wrote on last edited by
                #7

                I dont know how exactly u had written the class, I assumed some and written like this.. class A { public: long iSelPackageItemID; A() { iSelPackageItemID=100; } //and assigned the value in function... setPackageID() { iSelPackageItemID=200; } //then i have declared a function long getPackageID() { return iSelPackageItemID; } }; class B { public: A ob1; }; int main() { B ob2; A ob; cout< if ur code is similar to this than u got the solution. If this the right solution that ur problem is, u had given some valsue to iSelPackageItemID in the setPackageID() method, but when u create the onject to class A or B, ur members functions will not call and the valuse of setPackageID is not intialised, so give valuse to setPackageID either in constructor or call the setPackageID() function before calling getPackageID()<< method.

                1 Reply Last reply
                0
                • C chaitannya_m

                  I am not clear on this thing ... but now it looks like pretty much that it's going out of reference. My OOP fundas are weak... but just one question when i assign a value to a class member A, then i declare an obj of class B so when i try to access the member from a through B does it go out of scope..?

                  J Offline
                  J Offline
                  JudyL_MD
                  wrote on last edited by
                  #8

                  I'm not saying that the member itself of class A goes out of scope, I'm suggesting that what was assigned to the member went out of scope. The member itself is still good but it is referncing memory that is no longer good .You see this a lot with objects created on the stack. For example: char *MyCreateFunc () { char buffer[256] // do all the stuff to fill in buffer return buffer; } The statement A::m_memberVar = MyCreateFunc (); will compile and execute and may even work as expected sometimes. However, it is wrong!! A::m_memberVar remains in scope but what it points to is no longer in scope because the memory containing the data is no longer valid once the MyCreateFunc returns. Show the code where you create the value assigned to the member of class A. The member variables of a class remains in scope until the class itself goes out of scope, so unless you're accessing class A incorrectly, the problem has got to be in the value assigned to the member variable. Judy

                  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