Welcome, %1$s. Please login or register.

November 18, 2017, 01:41:01 PM
: 1
: Guess the output!  ( 1823 )
« : July 01, 2007, 11:41:26 AM From Poonam»



Guess the output!


#include <iostream.h>
 
class A
{
    public:
    void Print()
    {
         int i = 10;
         cout << "i= " << i;
    }
    private:
    int x;
};
 
void main()
{
    A* obj = NULL;
 
    obj->Print();
}

Why does the Print() function successfully prints i = 10 when the obj = NULL; ??

 
Liked It? Share it!

              


« #1 : July 19, 2007, 12:56:02 PM From Surender»

Here the pointer object is not destroyed when we equate it to NULL. It just deallocate the memory that is reserved for the instance variables i. e. x here. Since the function Print() do not refere/use the instance variable x it can execute successfully and print value of i. The member function memory area is shared and its not under a particular object so it obj=NULL wont affect it.
« #2 : November 20, 2007, 05:43:36 PM From iammilind»

Hi Surender,

   You are right, that function members are shared by the objects; while data members are owned by the objects. But there should not be any deallocation of memory for local objects or pointer, until they resides on stack. Deallocation is applicable only when we use free() or delete on heap objects. Moreover, consider the case of a class without any data member, there won't be any allocation-deallocation possible.

   The question can be answered in other words as following :
class A
{
public:  void Print(){ ... }
private: int x;
};

void main()
{
    A obj;
    obj.Print(); // calling function by object

    A *p;
    p->Print(); // calling function by pointer

    B *q;
    p->Print(); // Error

    ((A*)q)->Print();  // Ok ! It will work.
}


C++ compiler/parser will convert the above code to following at initial stage.

class A
{
    private: int x;
};
inline   // if function defined inside class or 'inline' declared
void Print(A *arg, ...) // "..." shows other arguments, if passed.
{ ... }

void main()
{
    A obj;
// first argument is always the address of object of the class ...
// ... which owns the function.
    Print(&obj, ...);

    A *p;
    Print(p, ...);  // p itself is the address

    B *q;
    Print(q, ...);  // will give error, because type of 'q' mismatches A*

    Print((A*)q, ...); // ok, type of 'q' matches A*
}


[Note: If the function was private for the class, the parser itself will not allow during compilation to call the function from outside class.]

Thus, it can be seen that, even though p is initialized to NULL, it is only an argument to the function. The value of p has no influence on the calling of function.

This is applicable to all shared members. Try with making int x as public and static. You can even access obj->x also without core dump (where obj=NULL).
: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer