|
In standard C++, you'd write it like this:
typeid(fruit1)==typeid(*fruit2);
PS: Ummm. The latter just checks whether fruit1 and fruit2 are of he same type. Determining if fruit1 is derived from the the type fruit2 seems a little harder. I'll think it over to try to find a solution.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
OK. To determine wheter fruit1 derives from the type of fruit2 , you have to know at compile time the exatc type of fruit2 . Suppose both objects are Fruit s and moreover fruit2 is an Apple . Then you'd write something like this:
dynamic_cast<Apple*>(fruit1)!=0 In C++, you can't get closer to the semantics of IsKindOf , but usually this is close enough, since you can do little with the type of an object ar run-time if you didn't know it before at compile-time --I hope I'm making myself clear enough.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
The only problem is that you need to enable RTTI and that increases the size of compiled code a lot. then you run into all kind of problems like linking with libraries without RTTI etc.
|
|
|
|
|
The only problem is that you need to enable RTTI and that increases the size of compiled code a lot.
Does it? I'd bet MFC's run-type schema adds a lot more code bloat to the app, and it is less efficient than RTTI, which after all is natively handled by the compiler. As for linking problems, you won't find any more problems as with any other RT schema external developers would have to implement anyway to be compatible with you.
Having RTTI switched off by default by VC++ is, to put it simply, non-standard, and a very bad decision IMHO.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín M López Muñoz wrote:
Does it? I'd bet MFC's run-type schema adds a lot more code bloat to the app, and it is less efficient than RTTI,
No this is not true. RTTI simply adds too much extra code. I will write an article on it soon.
Joaquín M López Muñoz wrote:
Having RTTI switched off by default by VC++ is, to put it simply, non-standard, and a very bad decision IMHO.
Even strourstrup recommends in his book not use RTTI.
|
|
|
|
|
No this is not true. RTTI simply adds too much extra code. I will write an article on it soon.
RTTI adds some bytes of information for each polymorphic class. I doubt this can generate much code bloat. Nevertheless, it'll be interesting to know what you come up with in your forthcoming article.
Even strourstrup recommends in his book not use RTTI.
Yep, but one thing is advising against RTTI and other recommending that RTTI be not included in the object code by default (which is, to say it once more, non-standard), or recommending that one writes RTTI replacements.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joel Holdsworth wrote:
fruit1->IsKindOf(fruit2->GetRuntimeClass());
Does this really tell you if fruit2 is of the same type as fruit1? Wouldn't this only tell you if fruit2 was of the same type _or an inherited type_ as fruit1?
Perhaps if you tell us what you want to accomplish there is a better way to do it? RTTI is almost always not the way you want to travel. Also be aware that MFC is often the blueprint of how not to do something.
|
|
|
|
|
Mike Nordell wrote:
RTTI is almost always not the way you want to travel.
Good point. I agree.
|
|
|
|
|
You can use RTTI but I don't recommend it. It adds lot of unecessary code. The easiest way for you would be to imitate what MFC does. i did have a set of classes to imitate this feature I will post it here if I find them.
|
|
|
|
|
I could partially agree with your advise of not using RTTI if avoidable, but imitating what MFC does (i.e. replacing RTTI with your own homemade schema) is usually much worse than relying in RTTI.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín M López Muñoz wrote:
but imitating what MFC does (i.e. replacing RTTI with your own homemade schema) is usually much worse than relying in RTTI.
Indeed. Fortunately there often are (way better) alternatives to do stuff than the MFC way. ![Smile | :)](https://codeproject.freetls.fastly.net/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|
Here is a simple implementation of RuntimeClass mechanism to give you an idea.
#include "assert.h"
class RuntimeObject;
class RuntimeClass
{
private:
RuntimeClass* m_pBaseClass;
private:
RuntimeClass()
: m_pBaseClass(NULL)
{
}
protected:
RuntimeClass(RuntimeClass* pBaseClass)
: m_pBaseClass(pBaseClass)
{
}
public:
virtual RuntimeClass* GetBaseClass()
{
return m_pBaseClass;
}
virtual RuntimeObject* CreateInstance();
friend class RuntimeObject;
};
class RuntimeObject
{
public:
RuntimeObject()
{
}
virtual RuntimeClass* GetRuntimeClass()
{
return &_runtimeClass;
}
bool IsKindOf(RuntimeClass* pClass);
static RuntimeClass _runtimeClass;
};
template<class T, class Base>
class RuntimeClassImpl : public RuntimeClass
{
public:
RuntimeClassImpl(RuntimeClass* pBaseClass)
: RuntimeClass(pBaseClass)
{
}
RuntimeObject* CreateInstance()
{
return new T();
}
};
template<class T, class Base>
class RuntimeObjectImpl : public Base
{
public:
RuntimeClass* GetRuntimeClass()
{
return &_runtimeClass;
}
static RuntimeClassImpl<T, Base> _runtimeClass;
};
template<class T, class Base>
RuntimeClassImpl<T, Base> RuntimeObjectImpl<T, Base>::_runtimeClass(&Base::_runtimeClass);
bool RuntimeObject::IsKindOf(RuntimeClass* pClass)
{
RuntimeClass* pThisClass = this->GetRuntimeClass();
while(pThisClass)
{
if (pThisClass == pClass)
return true;
pThisClass = pThisClass->GetBaseClass();
}
return false;
}
RuntimeClass RuntimeObject::_runtimeClass;
RuntimeObject* RuntimeClass::CreateInstance()
{
return new RuntimeObject();
}
//A derives from RuntimeObject
class A : public RuntimeObjectImpl<A, RuntimeObject>
{
};
//B derives from A
class B : public RuntimeObjectImpl<B, A>
{
};
//C derives from A
class C : public RuntimeObjectImpl<C, A>
{
};
int _tmain(int argc, _TCHAR* argv[])
{
C c;
A a;
B b;
assert(c.IsKindOf(a.GetRuntimeClass()));
assert(b.IsKindOf(a.GetRuntimeClass()));
assert(!c.IsKindOf(b.GetRuntimeClass()));
return 0;
}
|
|
|
|
|
Hah, you messed up "<" and ">"! :->
Maybe just copy it into e.g. notepad and replace < with & gt; and so on?
Don't mention it, I'm sure you'd do the same for me.
|
|
|
|
|
It happens to me every time. It happens with #include so I changed < to ". Now I need to take care of templates.
|
|
|
|
|
Rama Krishna wrote:
It happens with #include so I changed < to ". Now I need to take care of templates.
You might also check the [Display this message as-is (no HTML)] check-box
Then you can type < and > and whatever
<html>
<head>
<meta name="Generator" content="CP Post edit textbox">
<title>Nish</title>
</head>
<body>
<p> Hello Rama </p>
<a href='www.google.com'>google</a>
</body>
</html>
|
|
|
|
|
That's what I did when I modified the message.
|
|
|
|
|
take a look at "dynamic_cast". you can use it to do some basic runtime type info.
-c
Cheap oil. It's worth it!
|
|
|
|
|
Hello
I have a lot of text displayed in both static text and in edit boxes in a dialog.
I would like to make this information printable. I can't add OnPrint().... and those functions in my CDialog class. How do I get around this problem.
Regards
Henrik
|
|
|
|
|
I think this article will answer your question: http://www.codeproject.com/printing/printing_wo_docview.asp
With time we live, with money we spend!
Joel Holdsworth
|
|
|
|
|
Hello;
I have trouble with XML update process. I want to save XML document (in to a physical file) after update an element. But system generate, file in use error, when try to save the XML document. I know, i'm trying to save same file. Therefore, physical file has staying locked. Code is below;
XmlDocument* doc = new XmlDocument();
XmlNode* root;
XmlNode* node1;
doc->Load("C:\\Ege\\Lab7\\lab7.xml");
root = doc->DocumentElement;
node1= root->SelectSingleNode("a/b/c");
node1->LastChild->InnerText="15.95";
doc->Save("C:\\Ege\\Lab7\\lab7.xml");
The problem is, how can i save XML document into same file.
Thank you...
Orkun GEDiK
|
|
|
|
|
Put this before the Load() call:
doc->async = VARIANT_FALSE;
Otherwise Load() works asychronously and still might be accessing the file at the time you do the Save() call.
--Mike--
Just released - RightClick-Encrypt - Adds fast & easy file encryption to Explorer
Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Thank you Mike.
Orkun GEDiK
|
|
|
|
|
Let's say I have the following text in one cell of a
MS Flex Grid control:
important text
The only way I can add the word 'really' to the
beginning is by deleting the rest of the text so that
my cursor is not at the front and then re-typing all
of the text. Is there away when the user clicks on
a cell that they can then place the cursor somewhere
in that cell for quick editing? Any code? I looked
all over MSDN.
Please, any response any one can give me will be
greatly appreciated.
Sincerely,
Danielle (an overworked graduate student)
|
|
|
|
|
after i run a error free code in vc++ ,,application runs fine,,but when i try to exit the application it is saying MFC40.dll error..
what does it mean
can anyone of u can help me please
thanks in Advance
bye
|
|
|
|
|
It could be any number of problems, but a good place to start looking is how you are freeing your memory. You could be double deleting objects or something similar to that.
One other thing that you may try is to go into Debug | Exceptions on the menu, and change the settings so that anytime an exception occurs the program will be stopped.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|