Hi,

I am just trying to do some down casting.

here is the code.

class Base
{
public:
    void virtual test()
    {
        cout << "Base Test";
    }
};

class Derived : public Base
{
    int i;
    int j;
public:
    void testD()
    {
        i = 8;
        j = 9;
        cout << "Derived TestD";
    }
};

int _tmain(int argc, _TCHAR* argv[])
{	

	Base* pBase = new Base();

	// Case 1:
	Derived* pDerived = dynamic_cast<Derived*> (pBase);
	
	//this is causing crash (access violation). Perfectly valid derived dont have memroy for its data member
	pDerived->testD();

	// Case 2:
	Derived* pDerived1 = static_cast<Derived*> (pBase);
	
	//not causing crash (access violation). Executing fine. what is the reason????
	pDerived1->testD();

	return 0;
}

pDerived1->testD();
not causing crash (access violation). Executing fine. what is the reason????

Your Case 1 is not a "Perfectly valid Derived" it is a Base object that has been forced into a pointer to a Derived object. You can't call testD from a Base because a base has no testD method, virtual or otherwise, it has a test method. In fact, none of the Derived members exist in that object, because it wasn't given them when it was created.

In order for a virtual function to jump to a derived class's version of it, the derived class's version of the function must have an identical signature, which yours doesn't. The signatures don't match because the function names aren't the same.

class Base
{
public:
	void virtual test()
	{
		cout << "Base Test";
	}
};

class Derived : public Base
{
	int i;
	int j;
public:
	
	void test()
	{
		cout << "Derived Test";
	}

	void testD()
	{
		i = 8;
		j = 9;
		cout << "Derived TestD";
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	

	Base* pBase = new Base();

	// Case 1:
	Derived* pDerived = dynamic_cast<Derived*> (pBase);
	
	//this is causing crash (access violation). Perfectly valid derived dont have memroy for its data member
	pDerived->testD();

	// Case 2:
	Derived* pDerived1 = static_cast<Derived*> (pBase);
	
	//not causing crash (access violation). Executing fine. what is the reason????
	pDerived1->testD();

	return 0;
}

still working.

Actually my question is not related to virtual function. I just want to cast derived object to base object. Base doesnt know about derive. derived know about base. So derived's object in memory will have upper portion made of base and lower portion of itself.
Now if i am forcefully casting base pointer to derived then derived will have only upper portion that is of base not its own. So if there is no derived portion then its data member also wont have memory. So calling testD() function is valid because function gets separate shared memory in address space of process. I added 2 data members and modifying them in testD(). If derived dont have memory allocated to it then how its data member get modified.

In your first case, pDerived ends up being a NULL pointer. When you call TestD() through it, the this pointer will also be NULL (inside TestD() ) - hence the crash.

In the latter case, you are trashing memory worth of two ints approximately where your allocated Base object is stored - so, it certainly is not working by any means. Rather just bad luck that it is not crashing also.

ok. So what i am doing in second case is completely invalid and by luck it is working.
Thanks to both of you for solving.

Have you tried going through this with a debugger? Your dynamic cast is failing and placing a NULL pointer in pDerived. You are getting the access violation because you are attempting to dereference the NULL pointer. Something about your classes is incomplete, you'll have to figure out what that is to know why your dynamic_cast<> is failing. See attachment 1.

EDIT:
I did some more experimenting. I had suspicions earlier, but now I'm positive it has something to do with allocation for Derived::i and Derived::j. But I can't figure it out. If you remove i and j from Derived, the cast still returns a NULL, but there is no access violation. See attachment 2.

EDIT2:
Oops, just noticed a couple overlapped posts. I guess this one is irrelevant.

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.