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. Passing Pointers

Passing Pointers

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

    Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?

    B RaviBeeR P 3 Replies Last reply
    0
    • N NewHSKid

      Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

      Hello, You can do this: In class 1 add the next variable:

      Class1
      {
      Class2 * m_pClass2;
      // more stuff
      };

      Class2
      {
      // stuff of class 2
      };

      if you also want to have acess to private and protected members add the following to Class1:

      // stuff
      friend class Class2;
      // more stuff

      hope this helps :)

      A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

      N 1 Reply Last reply
      0
      • N NewHSKid

        Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?

        RaviBeeR Offline
        RaviBeeR Offline
        RaviBee
        wrote on last edited by
        #3

        Class1 instanceOfClass1; Class2 instanceOfClass2; // "m_ptrToClass1Object" is a public member of class Class2 instanceOfClass2.m_ptrToClass1Object = &instanceOfClass1; /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

        N 1 Reply Last reply
        0
        • B Bob Stanneveld

          Hello, You can do this: In class 1 add the next variable:

          Class1
          {
          Class2 * m_pClass2;
          // more stuff
          };

          Class2
          {
          // stuff of class 2
          };

          if you also want to have acess to private and protected members add the following to Class1:

          // stuff
          friend class Class2;
          // more stuff

          hope this helps :)

          A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

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

          Hi, thanks for the help. Does this work if the 2 classes are in different .cpp files? where exactly would this friend stuff go? thanks again

          B 1 Reply Last reply
          0
          • RaviBeeR RaviBee

            Class1 instanceOfClass1; Class2 instanceOfClass2; // "m_ptrToClass1Object" is a public member of class Class2 instanceOfClass2.m_ptrToClass1Object = &instanceOfClass1; /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

            N Offline
            N Offline
            NewHSKid
            wrote on last edited by
            #5

            Hello, thank you for helping me. Where would all of this go? I am lost.

            1 Reply Last reply
            0
            • N NewHSKid

              Hi, thanks for the help. Does this work if the 2 classes are in different .cpp files? where exactly would this friend stuff go? thanks again

              B Offline
              B Offline
              Bob Stanneveld
              wrote on last edited by
              #6

              The classes should be in header files! exemple:

              // the .h (header) file
              class a
              {
              public:
              void somefunc();
              };

              // in the .cpp (implementation) file
              void a::somefunc()
              {
              // code
              }

              But it will work if the 2 classes reside in other files! If you make a class fiend of an other class, That class has access to the private and protected members of that class: exemple:

              class a
              {
              private:
              int nPrivateInt;
              public:
              int nPublicInt;
              };

              class b
              {
              public:
              friend class a;
              void somefunc()
              {
              // do something
              a InstanceOfa;
              InstanceOfa.nPrivateInt = 0; // this is possible because a is a friend!
              }
              };

              // somewhere in the code
              a InstanceOfA;
              InstanceOfA.nPrivateInt = 0; // not possible because nPrivateInt is private!
              InstanceOfA.nPublicInt = 0; // possible because nPublicInt is public!

              Hope this makes thing clear!

              A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

              1 Reply Last reply
              0
              • N NewHSKid

                Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?

                P Offline
                P Offline
                PremL
                wrote on last edited by
                #7

                Suppose you have the following: class ClassB { public: void ActionB(); }; class ClassA { public: ClassB* m_pB; void ActionA(); }; Then you can access ClassB from ClassA using the following: ClassA a; //create an instance of each ClassB b; a.ActionA(); //call methods from A directly a.m_pB = &b; //pass a pointer to the instance of B to A a.m_pB->ActionB(); //call B through A using the stored pointer This is just one of many ways of doing this. The situation dictates what you actually implement. You need to learn about objects, instances, pointers, call by value, call by reference and lots of other concepts. I suggest you get yourself a good C++ book. Lorenz Prem Microsoft Corporation

                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