virtual function only works on dynamic declaration? (use of new)

This is a discussion on virtual function only works on dynamic declaration? (use of new) within the C++ Programming forums, part of the General Programming Boards category; I tried to find an answer on google but no luck.. considering this code... Code: #include <iostream> using namespace std; ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    Question virtual function only works on dynamic declaration? (use of new)

    I tried to find an answer on google but no luck..

    considering this code...

    Code:
    #include <iostream>
    
    using namespace std;
    
    class A
    {
    public:
           A(){}
           virtual void func(){cout<< "I'm A";}
          };
    
    class B : public A
    {
    public:
           B() : A() {}
           void func(){cout<<"I'm B";}
          };
    
    class XX
    {
    public:
          A a;
          XX() {}
    };
    
    class YY : public XX
    {
    public:
           YY(){
                  a = B();
                }
    };
    
    int main()
    {
        YY  y = YY();
        y.a.func();  //outputs I'm A
        
        cout<<std::endl;
        A* a = new B();
        a->func();   //outputs I'm B
        delete a;
        
        cout<< std::endl;
        
        A b = B();
        b.func(); //outputs I'm A
        
        system("PAUSE");
        return 0;
    }
    does virtual function only works on dynamically allocated (objects?)? you cant actually use it on b.func()?

    I'm long been working with C++ and yet it still have something that surprises me!

    thanks!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> does virtual function only works on dynamically allocated (objects?)

    of course not. it works on the actual type of the object.

    >> A b = B();

    the assignment there doesn't make b a B. it constructs an A object and then assigns it to a temporary B object.

    the main point is that virtual functions allow you to invoke the appropriate method even when the object is dereferenced with a base-class pointer. ie:

    Code:
    B b;
    A * bp = &b;
    bp->func(); //outputs I'm B
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    1
    Quote Originally Posted by Sebastiani
    >> A b = B();

    the assignment there doesn't make b a B. it constructs an A object and then assigns it to a temporary B object.
    Why?

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Sebastiani
    >> A b = B();

    the assignment there doesn't make b a B. it constructs an A object and then assigns it to a temporary B object.
    other way round I think. A temporary B object is created and assigned to an A object called "b". The B object will also be sliced.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21