Implementing virtual concept in c



 DEVELOP > c-Plus-Plus > Implementing virtual concept in c

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "mohan"
Date: 09 Jan 2006 12:23:15 AM
Object: Implementing virtual concept in c
Hi All,
How to implement virtual concept ( dynamic polymorphism ) in c.
I guess i should create a void pointer which is pointing to the function.
Not clear about this
Does anyone have some idea
Mohan
.

User: "Alf P. Steinbach"

Title: Re: Implementing virtual concept in c 09 Jan 2006 12:29:12 AM
* mohan:


How to implement virtual concept ( dynamic polymorphism ) in c.
I guess i should create a void pointer which is pointing to the function.

Don't use void pointers: you lose type checking, i.e. you introduce bugs.

Not clear about this
Does anyone have some idea

See <url: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>,
section 1.2 on "Run-time polymorphism".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "Jim Langston"

Title: Re: Implementing virtual concept in c 09 Jan 2006 01:27:29 AM
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:43c201f4.23566843@news.individual.net...

* mohan:


How to implement virtual concept ( dynamic polymorphism ) in c.
I guess i should create a void pointer which is pointing to the function.


Don't use void pointers: you lose type checking, i.e. you introduce bugs.


Not clear about this
Does anyone have some idea


See <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>,
section 1.2 on "Run-time polymorphism".

He said C, not C++.
I've seen something similar to polymorphism done in C with function
pointers. Although it didn't use void pointers, which are a bad thing, but
function pointers.
I may have the syntax off on this.
typedef (void MyFunc*)(int, float);
Which should (check syntax) create a function pointer called MyFunc which
points to a function returning a void taking 2 parameters, int and float.
Then you can at run time point this pointer to different functions depending
on what you want to do.
void MyRealFunc( int SomeInt, float SomeFloat)
{
std::cout << "In MyRealFunc" << std::endl;
}
void MyRealFunc2( int SomeInt, float SomeFloat)
{
std::cout << "In MyRealFunc2" << std::endl;
}
int main()
{
MyFunc = MyRealFunc;
MyFunc(1, 2.0);
MyFunc = MyRealFunc2;
MyFunc(1, 2.0);
return 0; // Not needed in main
}
Although this concept only works for functions, not classes as it would in
C++.
Thinking about it, I wonder if you couldn't create a pointer to a structure,
and the structure could have variables and function pointers itself
emulating C++'s class.
.
User: "Alf P. Steinbach"

Title: Re: Implementing virtual concept in c 09 Jan 2006 02:04:50 AM
* Jim Langston:

"Alf P. Steinbach" <alfps@start.no> wrote in message
news:43c201f4.23566843@news.individual.net...

* mohan:


How to implement virtual concept ( dynamic polymorphism ) in c.
I guess i should create a void pointer which is pointing to the function.


Don't use void pointers: you lose type checking, i.e. you introduce bugs.


Not clear about this
Does anyone have some idea


See <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>,
section 1.2 on "Run-time polymorphism".


He said C, not C++.

I know. That discussion is meant to show how to do this at the C level. If
it doesn't, then I'd be grateful for feedback so that I can fix it.
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.



User: "Jack Klein"

Title: Re: Implementing virtual concept in c 09 Jan 2006 10:34:38 PM
On Mon, 9 Jan 2006 11:53:15 +0530, "mohan" <mohan.bhakri@in.bosch.com>
wrote in comp.lang.c++:

Hi All,

How to implement virtual concept ( dynamic polymorphism ) in c.
I guess i should create a void pointer which is pointing to the function.
Not clear about this
Does anyone have some idea

1. Posts about C are off-topic here.
2. Why?
3. There is absolutely no defined conversion between void pointer and
any sort of function pointer in either C or C++.
4. If you want dynamic polymorphism, use C++. You must know it
exists, you found this group.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER