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. Advertising

  2. "Vinodh Kumar" <> wrote in message
    news:bg5eql$ftk$...
    > #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.
    >


    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. Advertising

  3. Vinodh Kumar

    David White Guest

    "Vinodh Kumar" <> wrote in message
    news:bg5eql$ftk$...
    > #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;


    Here you are slicing the A part off 'b' and copying the slice to 'a'.

    > (&a)->fa();


    Calls A::fa.

    >
    > // 2. Pointers
    > A a;
    > B b;
    > A* a1 = &b;


    Here you are simply taking the address of 'b'. You aren't copying from one
    object to another.

    > a1->fa();


    Calls B::fa.

    > */
    >
    > //3. References
    > A a;
    > B b;
    >
    > A& a1 = b;


    Here you are creating a reference to 'b'. Again, you aren't copying from one
    object to another.

    > a1.fa();


    Calls B::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.


    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

    "David White" <no.email@provided> wrote in message
    news:0zrVa.1519$...
    > "Vinodh Kumar" <> wrote in message
    > news:bg5eql$ftk$...
    > > #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;

    >
    > Here you are slicing the A part off 'b' and copying the slice to 'a'.
    >
    > > (&a)->fa();

    >
    > Calls A::fa.
    >
    > >
    > > // 2. Pointers
    > > A a;
    > > B b;
    > > A* a1 = &b;

    >
    > Here you are simply taking the address of 'b'. You aren't copying from one
    > object to another.
    >
    > > a1->fa();

    >
    > Calls B::fa.
    >
    > > */
    > >
    > > //3. References
    > > A a;
    > > B b;
    > >
    > > A& a1 = b;

    >
    > Here you are creating a reference to 'b'. Again, you aren't copying from

    one
    > object to another.
    >
    > > a1.fa();

    >
    > Calls B::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.

    >
    > 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
    >
    >
    >

    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

    "Vinodh Kumar" <> wrote in message
    news:bg5hao$pll$...
    >
    > "David White" <no.email@provided> wrote in message
    > news:0zrVa.1519$...
    > > "Vinodh Kumar" <> wrote in message
    > > news:bg5eql$ftk$...


    [snip]

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


    [snip]

    > > 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.
    > >

    > Any how the my 'A' object in "case 1" will have an address.When the

    contents
    > of this address is determined?


    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 contents 'a1' pointer in "case 2" can not be determined in compile
    > time?


    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.

    > In that case how one can say C++ is a statically typed language?


    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.

    > Or the
    > contents of 'a' can not be determined at run time?


    '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. 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. Roger Leigh
    Replies:
    8
    Views:
    437
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Marc Thrun
    Replies:
    15
    Views:
    859
    Tim Rentsch
    Oct 4, 2005
  3. Alfonso Morra
    Replies:
    3
    Views:
    366
    Alfonso Morra
    Jul 21, 2005
  4. Gary Wessle
    Replies:
    2
    Views:
    286
    Salt_Peter
    Nov 10, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
Loading...

Share This Page