Type Casting in C++


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Cint
{
    int a,b;
public:
void display()
{
  cout<<a<<b<<endl;
}
};

class Cchar
{
   char x;

void show()
{
 cout<<x<<endl;
}
};


int main()
{
Cint*Cptr=(Cint*)new Cchar();
Cptr->display();
return 0
}

I am clueless what actually happens here as CPtr belongs to Cint class which has 2 int data members and display member function and the address is of the object which is of type Cchar which has one char data member and show function.

1.After this assignment Cint*Cptr=new Cchar()Cptr should be treated like Cint type pointer or Cchar type pointer? (which is valid Cptr->display() or Cptr->show())?

2.Also shall I use below statement in main cout<<Cptr->a<<CPtr->b<<Cptr->x?

Please help.i request not provide base and derived type conversion examples as thats little confusing for me and I want to understand through this example how this pointer conversions will happen internally.
So basically you have two classes:
1
2
3
4
5
6
7
8
9
10
11
class Cint {

public:
	
	int a, b;
	
	void display()
	{
	  cout<<a<<b<<endl;
	}
};

1
2
3
4
5
6
7
8
9
10
11
class Cchar {

public:

	char x;

	void show()
	{
		cout<<x<<endl;
	}
};


Then you are casting the Cchar to the Cint type:
 
Cint * Cptr = (Cint*)new Cchar();


The class still retains its Cint type. Therefore, you should use Cint's function and variable members.

Casting, in the most basic terms, means that you are converting one type variable to another type.

I believe that if you give Cchar's x member a value, it will be stored into Cint's a variable. I think this is because a character uses 1 byte and an integer uses 4 bytes. Therefore, the a value will have enough space to store the x value.
YOu have used an unsafe C-style cast. In this case it works as reinterpret_cast: pointer to one class threated as pointer to another. But it still points to instance of first class. If those classes are not binary compatible on given platform with given compiler with given setings, this causes undefined behavior. In your case (x86 platform) it will probably just print junk if you read int values (aside from rare occurence when object allocated at the edge of allocated memory page), but writing to it may corrupt other data.

For your question: It is a Cint pointer and will be threated as such (yo cannot access x member variable through it) but it actually points to Cchar object and you should not use this pointer, because classes are not comparible at all.
Also you should not use delete Cptr, because it will try to deallocate Cchar object using Cint destructor. Correct way would be delete reinterpret_cast<Cchar*>(Cptr);

Do not use C-style casts either. They are silet about user errors. In your case they allowed you to convert pointers to unrelated classes, which is what you should not do, unless you are very well versed in C++ and know exactly how member variables are stored in memory in C++.
Topic archived. No new replies allowed.