Implementing isInstanceOf(...) in c, any ideas

D

dwaach

Hi,

I am thinking about implementing isInstanceOf() in c.
Any ideas ???

Ex.

int findType(struct x)
{
struct X orgX;
if (isInstanceOf(x, orgX))
return 1;
else
return 0;
}

main()
{
struct Y x;
int val=0;
val=findType(x);
}



Regards,
Abhishek
 
P

Protoman

dwaach said:
Hi,

I am thinking about implementing isInstanceOf() in c.
Any ideas ???

Ex.

int findType(struct x)
{
struct X orgX;
if (isInstanceOf(x, orgX))
return 1;
else
return 0;
}

main()
{
struct Y x;
int val=0;
val=findType(x);
}



Regards,
Abhishek

Well, there's always RTTI...
 
I

Ivan Vecerina

: I am thinking about implementing isInstanceOf() in c.
: Any ideas ???
C has no built-in concept of classes or instances,
so the answer would depend on how you choose to emulate
that feature.
In C++, dynamic_cast can be used to find if an object
is of a given subclass. You could write isInstanceOf
as follows:
template<class D, class B>
bool isInstanceOf(const B& b)
{ return !! dynamic_cast<const D*>(&b); }

Usage example (using <fstream>):

bool isFileOutput(std::eek:stream& o)
{
return isInstanceOf<std::eek:fstream>(o);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top