Help regarding virtual function.

Discussion in 'C++' started by trialproduct2004@yahoo.com, Apr 26, 2006.

  1. Guest

    Hi all,

    Can someone tell me how virtual functions works.

    Like how memory is getting allocated to virtual function. And how to
    call base class function through derived class pointer.
    And why virtual function can be access only through pointer.

    Thanks in advance.
     
    , Apr 26, 2006
    #1
    1. Advertising

  2. * :
    >
    > Can someone tell me how virtual functions works.


    The nearest C++ textbook.

    You can try

    #include <iostream>
    #include <ostream>

    void say( char const s[] ) { std::cout << s << std::endl; }

    struct Base
    {
    virtual void foo() const { say( "Base::foo" ); }
    };

    struct Derived: Base
    {
    virtual void foo() const { say( "Derived::foo" ); }
    };

    int main()
    {
    Base const& baseRef = Derived();
    baseRef.foo();
    }


    > Like how memory is getting allocated to virtual function.


    It isn't.


    > And how to call base class function through derived class pointer.


    p->Base::foo();


    > And why virtual function can be access only through pointer.


    There's no such restriction.

    --
    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, Apr 26, 2006
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > Hi all,
    >
    > Can someone tell me how virtual functions works.
    >
    > Like how memory is getting allocated to virtual function.


    It's implementation dependant, but usually (if not always) you only have one
    copy of the function in the executable. That's why methods usually don't
    add to an objects size.

    On my system this program outputs
    4 4

    #include <iostream>
    #include <string>

    class Class1
    {
    int i;
    };

    class Class2
    {
    int i;
    void Output() { std::cout << i; };
    };

    int main ()
    {

    std::cout << sizeof(Class1) << " " << sizeof(Class2);

    std::string wait;
    std::cin >> wait;
    }

    Methods do not usually add to a class's size for storage. They are just an
    executable function in the executable usually called through some lookup
    table.

    > And how to
    > call base class function through derived class pointer.


    Study the output of this program.

    #include <iostream>
    #include <string>

    class Base
    {
    public:
    virtual void Output() { std::cout << "Base" << std::endl; };
    };

    class Derived: public Base
    {
    public:
    void Output() { std::cout << "Derived" << std::endl; Base::Output(); };
    };

    int main ()
    {
    Derived Instance;
    Base* BaseP = &Instance;

    std::cout << "Derived call of Output()" << std::endl;
    Instance.Output();
    std::cout << "Base Pointer call of Output()" << std::endl;
    BaseP->Output();
    std::cout << "Base Pointer call of Base::Output()" << std::endl;
    BaseP->Base::Output();

    std::string wait;
    std::cin >> wait;
    }

    Output:
    Derived call of Output()
    Derived
    Base
    Base Pointer call of Output()
    Derived
    Base
    Base Pointer call of Base::Output()
    Base


    > And why virtual function can be access only through pointer.


    Look at the above program. As you see, Instance which is an instance of the
    derived class has it's virtual function accesed and it's not a pointer.
    What do you mean?
     
    Jim Langston, Apr 27, 2006
    #3

  4. > Can someone tell me how virtual functions works.
    >
    > Like how memory is getting allocated to virtual function. And how to
    > call base class function through derived class pointer.
    > And why virtual function can be access only through pointer.


    The following article should help:
    http://www.eventhelix.com/RealtimeMantra/basics/ComparingCPPAndCPerformance2.htm

    --
    EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
    Sequence Diagram Based System Design and Object Interaction Modeling
    Tool
     
    EventHelix.com, Apr 27, 2006
    #4
    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. Xiangliang Meng
    Replies:
    2
    Views:
    426
    Jack Klein
    Jun 21, 2004
  2. IK
    Replies:
    2
    Views:
    639
    hemraj
    Jul 23, 2004
  3. Ashwin
    Replies:
    2
    Views:
    368
    Pierre Barbier de Reuille
    Aug 1, 2006
  4. Replies:
    11
    Views:
    723
    James Kanze
    Sep 10, 2006
  5. somenath
    Replies:
    2
    Views:
    179
    somenath
    Aug 29, 2013
Loading...

Share This Page