By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

dwaach wrote:
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...

Jul 10 '06 #2

P: n/a
I am thinking about implementing isInstanceOf() in c.
Any ideas ???
Post in comp.lang.c instead of c++ ? :)

Jul 10 '06 #3

P: n/a
"dwaach" <xb**************@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
: 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::ostream& o)
{
return isInstanceOf<std::ofstream>(o);
}

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Jul 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.