Scope of pointer to class instance
-
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}
IntegerABC
is limited in scope to function SomeFunction(). Is there any limitation to the scope ofpYY
? CanpYY
be used in any other function other thanSomeFunction()
? Thanks -
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}
IntegerABC
is limited in scope to function SomeFunction(). Is there any limitation to the scope ofpYY
? CanpYY
be used in any other function other thanSomeFunction()
? ThanksThe scope of the variable
pYY
itself is limited toX::SomeFunction
. However, given that the object thatpYY
is pointing to has been allocated on the heap that object continues to exist beyondX::SomeFunction
. You could for instance returnpYY
fromX::SomeFunction
and use it at the caller site.-- gleat http://blogorama.nerdworks.in[^] --