Re: virtual functions and optimization at compile time

tmk@netvision.net.il (Michael Tiomkin)
3 Dec 2003 20:21:35 -0500

          From comp.compilers

Related articles
virtual functions and optimization at compile time pdn@sasken.com (preetham) (2003-11-21)
Re: virtual functions and optimization at compile time sasulzer@seanet.com (Stephen Sulzer) (2003-12-03)
Re: virtual functions and optimization at compile time joachim.durchholz@web.de (Joachim Durchholz) (2003-12-03)
Re: virtual functions and optimization at compile time hannah@schlund.de (2003-12-03)
Re: virtual functions and optimization at compile time tmk@netvision.net.il (2003-12-03)
Re: virtual functions and optimization at compile time alexc@std.com (Alex Colvin) (2003-12-03)
| List of all articles for this month |
From: tmk@netvision.net.il (Michael Tiomkin)
Newsgroups: comp.compilers
Date: 3 Dec 2003 20:21:35 -0500
Organization: http://groups.google.com
References: 03-11-076
Keywords: OOP, optimize
Posted-Date: 03 Dec 2003 20:21:35 EST

"preetham" <pdn@sasken.com> wrote in message news:03-11-076...
> Virtual Functions Are Supposed To Be Optimized Bcoz They R > Supposedly
> Resolved At Run-Time. How Can Virtual Functions Be Resolved At
> Compile-Time???


    When an object is created in the program, the compiler knows the
type of this object, and then the compiler can find any virtual method
of its class. When an object is a reference/ptr obtained from the
outside, the exact type of the object cannot be determined, and the
virtual methods are found at runtime. For example:


class A
{
  ...
    virtual int a();
  ...
  };


  void func(A x, A& y, A* z)
    {
      A u;
      A* t = new A;
      A& v = u;
      A& s = *z;
      A* w = ff(u);


      x.a(), y.a(), z->a(), u.a(), v.a(), t->a(), s.a(), w->a();


      // the type of x, u, t, and v is 'A', and the address of 'a'
      // is known at compile time;
      // the types of y, z, s, and w can be A or any descendant of A,
      // and the address of 'a' can be determined only at runtime


> Rgrds,
> Preetham
> [By flow analysis that determines that it'll always call the same
> function, I suppose. -John]



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.