Objects, Pointers and References

Discussion in 'C++' started by Vinodh Kumar, Jul 29, 2003.

  1. Vinodh Kumar

    Vinodh Kumar Guest

    #include <iostream>
    using namespace std;

    struct A
    {
    virtual void fa();
    };


    struct B : public A
    {
    virtual void fa();
    };


    void A::fa()
    {
    cout << "A::fa" << endl;
    }


    void B::fa ()
    {
    cout << "B::fa" << endl;
    }

    int main()
    {
    /*
    // 1. Normal objects
    A a;
    B b;
    a = b;
    (&a)->fa();


    // 2. Pointers
    A a;
    B b;
    A* a1 = &b;
    a1->fa();
    */

    //3. References
    A a;
    B b;

    A& a1 = b;
    a1.fa();

    return 0;
    }


    In the above program within main( ) inside the commented block // 1. Normal
    objects,
    a = b;
    (&a)->fa();
    does not make an invocation to B::fa();Why is that?Then what the above
    assignment does in the same block.Shed some light please.


    Regards,
    Vinodh Kumar P
     
    Vinodh Kumar, Jul 29, 2003
    #1
    1. Advertisements

  2. It converts your B object into a A object. So when you call (&a)->fa(); all
    you have is an A object. This is sometimes called object slicing.

    The bottom line is that if you want to use virtual functions in C++, you
    have to use pointers or references.

    john
     
    John Harrison, Jul 29, 2003
    #2
    1. Advertisements

  3. Vinodh Kumar

    David White Guest

    Here you are slicing the A part off 'b' and copying the slice to 'a'.
    Calls A::fa.
    Here you are simply taking the address of 'b'. You aren't copying from one
    object to another.
    Calls B::fa.
    Here you are creating a reference to 'b'. Again, you aren't copying from one
    object to another.
    Calls B::fa.
    An object of class A cannot metamorphose into an object of class B. Once an
    A always an A. So the function called in 1. is naturally A::fa. In the other
    cases you were taking the address of and reference to a B object. So the
    function called was naturally B::fa.

    DW
     
    David White, Jul 29, 2003
    #3
  4. Vinodh Kumar

    Vinodh Kumar Guest

    Any how the my 'A' object in "case 1" will have an address.When the contents
    of this address is determined?
    The contents 'a1' pointer in "case 2" can not be determined in compile
    time?In that case how one can say C++ is a statically typed language?Or the
    contents of 'a' can not be determined at run time?
     
    Vinodh Kumar, Jul 29, 2003
    #4
  5. Vinodh Kumar

    David White Guest

    The contents are what they are. It's an A object, so it will be set up to
    call A::fa when called via a run-time virtual call.
    The decision of what function to call in all three cases is made at
    run-time, since in all three cases you are calling a virtual function
    through a pointer or reference.
    I'm not sure how a language lawyer would answer this, but to me it has a
    mixture of static and dynamic types. Actual objects and member access of
    objects are statically typed. Virtual calls through pointers and references,
    dynamic_cast and other run-time type support are dynamically typed.
    'a' is an object of type A, so it can only behave like an A no matter how
    you access it.

    DW
     
    David White, Jul 29, 2003
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.