please help me understand the folowing code....

class A {
      int a;
  public:
     A() : a ( 0 ) {} 
     int get()  {
          cout << "Hello WOrld " << endl;
          return 0;
     }
 };
 
  int main( int argc, char** argv )
 {
      cout<< ((A*)3)->get() << endl;
       return 0;
 }

this code runs! and prints the correct output if i return zero in get() function,, but when i return a it throws an exception ( i think becuase a is not initialized, infact not constructed ), but this code runs ......no idea:eek:

please help me understand the folowing code....

class A {
      int a;
  public:
     A() : a ( 0 ) {} 
     int get()  {
          cout << "Hello WOrld " << endl;
          return 0;
     }
 };
 
  int main( int argc, char** argv )
 {
      cout<< ((A*)3)->get() << endl;
       return 0;
 }

The thing is that when an object is created, its constructor is called but not when the object pointer is created which is the thing in your case. (please get this verified, havent done C++ in quite a while)

So as a pointer to object is created, you dont specifically allocate any memory, the class variable doesnt exist. So as soon as the function si encountered, the "cout" stmt is executed and prints out the output but when encounters the reutrn a stmt, there is as such no "a" to which the object points to since no memory has been allocated.

In the end, yes since the memory is not allocated for the pointer and neither does it point to any object hence an execption is generated. Returning something which doesnt exist is not a very good thing ;)
And btw your code doesnt technically run as an run time exception is generated, there is a difference you know ;)

Hope it helped, bye.

>>cout<< ((A*)3)->get() << endl;

This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.

A* pA = new A; // allocate an instance of the class
pA->get();
// 
// and don't forget to deallocate it
delete pA;

Another way to do it is to make the pointer point to an already existing object of the class

A a; // an instance of class A
A* pA = &a; // pointer to the object
//
//
// you do NOT delete the pointer

>>cout<< ((A*)3)->get() << endl;

This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.

Oh yes and i overlooked this particular thing. Thanks for pointing it out Mr. Dragon. Maybe this is one of those quizzies questions where you are asked to predict the output of the code snippet.

Rule:
1. Before using any pointer dont forget to allocate memory to it or to point it to an existing object. Hence when using a pointer better ground it (make it NULL) so that you dont end up using it by mistake.

2. Delete only those pointers for those you have explicitly allocated memory using the "new" stmt. Forgettting to free the memory results in what is known as a memory leak.

Hope it helped, bye.

heyz it its undefined what about the following code

class  A { 
  int a;
public:
  A() : a ( 0 )  {}
 
 int get() const {
       cout <<" in A()" << endl;
       return a; 
 }
};
 
int main()
{
       *((int*)0x00100ff)=14;
       cout<< reinterpret_cast<A*>(0x00100ff)->get() << endl;
       return 0;
}

it works ..........:)

yes its using undefined behavior too. You can't just hardcode an address into the program like that and expect it to work because only the computer knows where an object's address, unless you are using some obscure operating system such as some embedded systems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.