Hello,
when working with C#, you can determine the type of a variable at runtime.
Let's say we have something like the following (in C#):
public class A
{
// whatever
}
public class B : A
{
// whatever
}
public class C
{
// whatever
}
public class MainClass
{
public static void doSomeTask() {}
public static void doOtherTask() {}
public static void Main(string[] args)
{
A a = new A;
B b = new B;
C c = new C;
if (typeof(a) == typeof(b))
{
doSomeTask();
}
if (typeof(a) == typeof(c))
{
doOtherTask();
}
}
}
In the example given, the function doSomeTask will be executed, because they
have the same type: b is derived from A, hence they
"share" the same type and the comparisson returns true. Whereas, doOtherTask
will never be executed, because nither c is of type A, nor c is derived from
A. Thus the comparisson returns false.
This is sometimes usefull when dealing with inheritance, when you want to
know, given a pointer to a base class, the type of the derived class.
Now, I know that in C++ exists the operator typeid. Similar to C#'s typeof,
typeid returns a structure of type_info. So -and because type_info has two
overloaded operators (== and !=)- I can compare two types:
CClass c1;
CClass c2;
if (typeid(c1) == typeid(c2))
{
cout << "They are equal" << endl;
}
My question is whether it is possible to do something like the C# example
(using inheritance) with the typeid operator; or if there's a way to doing
this in C++.
Regards and a happy new year,
Fernando G�mez.
------------------------ Yahoo! Groups Sponsor --------------------~-->
$4.98 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/Q7_YsB/neXJAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~->
To unsubscribe : [EMAIL PROTECTED]
Yahoo! Groups Links
<*> To reply to this message, go to:
http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=4948
Please do not reply to this message via email. More information here:
http://help.yahoo.com/help/us/groups/messages/messages-23.html
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Programmers-Town/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/