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. Scope of pointer to class instance

Scope of pointer to class instance

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
2 Posts 2 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.
  • O Offline
    O Offline
    Oliver123
    wrote on last edited by
    #1

    If I define a variable in a function, its scope remains local to that function. Suppose I define a new instance of a class and a pointer in a function. Is the pointer local to the function? Can it be referenced outside the function? Do I have to declare the pointer in the header if I want to use it outside the function? For example: X::SomeFunction() { int ABC = 0; Y* pYY = new Y; //Creates new instance of class Y } Integer ABC is limited in scope to function SomeFunction(). Is there any limitation to the scope of pYY? Can pYY be used in any other function other than SomeFunction()? Thanks

    R 1 Reply Last reply
    0
    • O Oliver123

      If I define a variable in a function, its scope remains local to that function. Suppose I define a new instance of a class and a pointer in a function. Is the pointer local to the function? Can it be referenced outside the function? Do I have to declare the pointer in the header if I want to use it outside the function? For example: X::SomeFunction() { int ABC = 0; Y* pYY = new Y; //Creates new instance of class Y } Integer ABC is limited in scope to function SomeFunction(). Is there any limitation to the scope of pYY? Can pYY be used in any other function other than SomeFunction()? Thanks

      R Offline
      R Offline
      Rajasekharan Vengalil
      wrote on last edited by
      #2

      The scope of the variable pYY itself is limited to X::SomeFunction. However, given that the object that pYY is pointing to has been allocated on the heap that object continues to exist beyond X::SomeFunction. You could for instance return pYY from X::SomeFunction and use it at the caller site.

      -- gleat http://blogorama.nerdworks.in[^] --

      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