Skip to main content
GameDev.net gamedev.net
🔒 Locked

problem with typeid()

Started by Yanroy Jan 23, 2005 at 7:27 PM 2 replies 1.4k views
Original Post
Yanroy
Yanroy
I'm having a strange problem where typeid() lies to me. It insists that a pointer I'm giving it is a pointer to a base class when it's actually a pointer to a derived class. Yes, I'm dereferencing the pointer. I'm using almost identical code (it has the same form, but it involves different classes) elsewhere in my program and it works fine. Here's the applicable code: a CSharedFileInfo instance is created and passed to a function that takes a CSharedFSObject pointer:

CSharedFileInfo* newFile = new CSharedFileInfo(&FileData);
AddFSObject(newFile);
this is the function with a couple comments to explain the issue:

void CSharedFileDir::AddFSObject(CSharedFSObject* FSObj)
{
	// note: inserts at the top of the list, because it's faster

	Node* NewNode = new Node;
	NewNode->FSObj = FSObj;

	// the list is empty, just point the head at the new node
	if(!Head)
	{
		Head = NewNode;
	}
	else
	{
		// replace and point to the old head
		Node* NextPtr = Head;
		Head = NewNode;
		NewNode->Next = NextPtr;
	}

// !!!!!! - THIS IF STATEMENT FAILS:
	// if it's a file, not a dir, increment the file counter
	if(typeid(*FSObj) == typeid(CSharedFileInfo))
		NumFiles++;

// THIS MESSAGE BOX SAYS "class CSharedFSObject":
	AfxMessageBox(typeid(*FSObj).name(), MB_OK, NULL);

	return;
}
and if you need proof that CSharedFileInfo is a child of CSharedFSObject:
class CSharedFileInfo : public CSharedFSObject
Any help will be appreciated! This one has been driving me nuts for hours.
--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Yanroy
Yanroy
I have RTTI on. You may have noticed I said I have similar code in my program that works perfectly... There's something deeper going on here.
--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
DudeMiester
DudeMiester
My only guess is that the class has to have a virtual function in order for that to work. Not sure.
[s] [/s]
I can see the fnords.
ITGER
ITGER
typeid only checks the EXACT TYPE of the object, it doesn't check the is-a relationship. So if you have:

A *pA = new B();

where A is the supertype, then you do
typeid(*pA) == typeid(A)

this will fail, even though B is derived from A.

typeid(*pA) == typeid(B) will pass

Edit-
Oh yeah the guy above me is correct, RTTI only works on polymorphic types, so you need some virtual function(s) that get overridden in your derived class for it to treat it correctly. You may want to give more information about your class hierarchy. The weird thing is, you just need one virtual function to do the trick, and if this isn't working, assuming because you don't have any virtual functions, it means YOU DON'T HAVE A VIRTUAL DESTRUCTOR for your base class? oh boy...please add one!!!

[Edited by - ITGER on January 23, 2005 11:47:02 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.