Implementing virtual concept in c

Discussion in 'C++' started by mohan, Jan 9, 2006.

  1. mohan

    mohan Guest

    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
    mohan, Jan 9, 2006
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Jan 9, 2006
    #2
    1. Advertising

  3. mohan

    Jim Langston Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    >* 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.
    Jim Langston, Jan 9, 2006
    #3
  4. * Jim Langston:
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    > >* 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?
    Alf P. Steinbach, Jan 9, 2006
    #4
  5. mohan

    Jack Klein Guest

    On Mon, 9 Jan 2006 11:53:15 +0530, "mohan" <>
    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
    Jack Klein, Jan 10, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mshetty
    Replies:
    14
    Views:
    4,486
    Mike Bland
    Sep 10, 2004
  2. Chetan Raj
    Replies:
    2
    Views:
    429
    Alf P. Steinbach
    Nov 21, 2005
  3. v4vijayakumar
    Replies:
    7
    Views:
    407
    Salt_Peter
    Apr 11, 2007
  4. Jaco Naude
    Replies:
    5
    Views:
    570
    Jaco Naude
    Apr 1, 2009
  5. earthwormgaz
    Replies:
    3
    Views:
    345
    Michael Doubez
    Mar 11, 2010
Loading...

Share This Page