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. call by reference using pointers

call by reference using pointers

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

    does anyone know how to write this as call by constant reference using pointers?? here is what ive been trying to do: int CComputer::CheckForMoves(const CBoard& rBoard, int depth) { CBoard Board = rBoard; <-- but using pointers instead of reference } so i can call it in the main function like this: CheckForMoves(pBoard,1); where pBoard is a pointer of CBoard is there a way i can do something like that without getting any syntax errors???? cause i have tried all sorts of stuff like that but the compiler wont allow me to do so.... anyways, thanx for you help :) John

    P R M J 4 Replies Last reply
    0
    • J John Cruz

      does anyone know how to write this as call by constant reference using pointers?? here is what ive been trying to do: int CComputer::CheckForMoves(const CBoard& rBoard, int depth) { CBoard Board = rBoard; <-- but using pointers instead of reference } so i can call it in the main function like this: CheckForMoves(pBoard,1); where pBoard is a pointer of CBoard is there a way i can do something like that without getting any syntax errors???? cause i have tried all sorts of stuff like that but the compiler wont allow me to do so.... anyways, thanx for you help :) John

      P Offline
      P Offline
      Prem Kumar
      wrote on last edited by
      #2

      CheckForMoves( *pBoard,1) ;//main fn int CCtrlResize::CheckForMoves(const CBoard& rBoard, int depth) { const CBoard& Board = rBoard; //Init to a const referece, because thats what it is return 0 ; }

      R 1 Reply Last reply
      0
      • J John Cruz

        does anyone know how to write this as call by constant reference using pointers?? here is what ive been trying to do: int CComputer::CheckForMoves(const CBoard& rBoard, int depth) { CBoard Board = rBoard; <-- but using pointers instead of reference } so i can call it in the main function like this: CheckForMoves(pBoard,1); where pBoard is a pointer of CBoard is there a way i can do something like that without getting any syntax errors???? cause i have tried all sorts of stuff like that but the compiler wont allow me to do so.... anyways, thanx for you help :) John

        R Offline
        R Offline
        Rickard Andersson20
        wrote on last edited by
        #3

        What I know that is not allowed! You have to use either references or pointers. ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

        1 Reply Last reply
        0
        • P Prem Kumar

          CheckForMoves( *pBoard,1) ;//main fn int CCtrlResize::CheckForMoves(const CBoard& rBoard, int depth) { const CBoard& Board = rBoard; //Init to a const referece, because thats what it is return 0 ; }

          R Offline
          R Offline
          Rickard Andersson20
          wrote on last edited by
          #4

          Well, I guess I got it wron then! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

          1 Reply Last reply
          0
          • J John Cruz

            does anyone know how to write this as call by constant reference using pointers?? here is what ive been trying to do: int CComputer::CheckForMoves(const CBoard& rBoard, int depth) { CBoard Board = rBoard; <-- but using pointers instead of reference } so i can call it in the main function like this: CheckForMoves(pBoard,1); where pBoard is a pointer of CBoard is there a way i can do something like that without getting any syntax errors???? cause i have tried all sorts of stuff like that but the compiler wont allow me to do so.... anyways, thanx for you help :) John

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

            Pointers and references are not the same thing, you can't use both at the same time*. You can either write:

            int CComputer::CheckForMoves(const CBoard& rBoard, int depth)
            {
            const CBoard& Board = rBoard;
            const CBoard* p = &rBoard;
            }

            which passes the param as a reference, or:

            int CComputer::CheckForMoves(const CBoard* pBoard, int depth)
            {
            const CBoard* p = pBoard;
            const CBoard& refBoard = *pBoard;
            }

            which passes a pointer. * Note for C++ experts: yes I know you can have a reference to a pointer; I didn't mention that above because it would be unnecessarily confusing. --Mike-- Best score on the mini-putt game: 27 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan and Jamie Salé.

            J 1 Reply Last reply
            0
            • M Michael Dunn

              Pointers and references are not the same thing, you can't use both at the same time*. You can either write:

              int CComputer::CheckForMoves(const CBoard& rBoard, int depth)
              {
              const CBoard& Board = rBoard;
              const CBoard* p = &rBoard;
              }

              which passes the param as a reference, or:

              int CComputer::CheckForMoves(const CBoard* pBoard, int depth)
              {
              const CBoard* p = pBoard;
              const CBoard& refBoard = *pBoard;
              }

              which passes a pointer. * Note for C++ experts: yes I know you can have a reference to a pointer; I didn't mention that above because it would be unnecessarily confusing. --Mike-- Best score on the mini-putt game: 27 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan and Jamie Salé.

              J Offline
              J Offline
              John Cruz
              wrote on last edited by
              #6

              hey thanks everyone for help me. ill try all your suggestions to see it would work. thank you very much again :)

              1 Reply Last reply
              0
              • J John Cruz

                does anyone know how to write this as call by constant reference using pointers?? here is what ive been trying to do: int CComputer::CheckForMoves(const CBoard& rBoard, int depth) { CBoard Board = rBoard; <-- but using pointers instead of reference } so i can call it in the main function like this: CheckForMoves(pBoard,1); where pBoard is a pointer of CBoard is there a way i can do something like that without getting any syntax errors???? cause i have tried all sorts of stuff like that but the compiler wont allow me to do so.... anyways, thanx for you help :) John

                J Offline
                J Offline
                Joaquin M Lopez Munoz
                wrote on last edited by
                #7

                Try CheckForMoves(*pBoard,1). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                J 1 Reply Last reply
                0
                • J Joaquin M Lopez Munoz

                  Try CheckForMoves(*pBoard,1). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  J Offline
                  J Offline
                  John Cruz
                  wrote on last edited by
                  #8

                  hey Joaquin... thank you it worked... :):):) been think bout this the whole day and i didnt try putting a pointer like CheckForMoves(*pBoard,1).... i think i overlooked that part... muchas gracias mi amigo por me ayudo...

                  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