是一个纯虚函数实际上在过载分辨率中选择? [英] Is a pure virtual function actually selected in overload resolution?

查看:143
本文介绍了是一个纯虚函数实际上在过载分辨率中选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我在上一个问题的评论:


由于不能有抽象类的实例,纯虚函数可以


明显的响应是:

  abstract_class * a = new derived_class; a-> pure_virtual_function(); 

以及正确性的最佳证明:



< blockquote>

动态分派在运行时发生,基于当时实际使用的对象。







然而,令我烦恼的是,当在我们的情况下显式地解析类成员的范围编译失败,所以它看起来像一个纯虚函数从来没有通过重载解析实际选择: p>

  struct B 
{
virtual void copy(B const& rhs)= 0;
};

struct D:B
{
void copy(B const& rhs)
{
D const * pd = dynamic_cast< D const *> (& rhs);
if(pd){
y = pd-> y;
}
}
int y;
};

int main()
{
D d1,d2;
d1.y = 2;
d2.y = 5;

B * p1(& d1),* p2(& d2);

////////////////////////////////////////// //
(* p1).B :: copy(* p2);
////////////////////////////////////////////

return 0;
}



错误讯息



< >

未定义引用`B :: copy(B const&)'


演示



这里的情况是什么,如果纯虚函数实际上是在重载解析中选择的,为什么我不能强制编译器做同样的事情(作为分辨率)。

解决方案

纯虚函数可以通过重载分辨率来选择。在大多数情况下,它不会被调用。

  struct A 
{
virtual void foo()= 0;
void foo(int);
};

A * getA();

int main()
{
A * a = getA();
a-> foo();
}

struct B:A
{
void foo(){}
};
A * getA()
{
return new B;
}

重载解析选择 A :: a / code>( B :: a()),然后 B :: a (),并在运行时由虚拟分派机制调用。纯度在这里是无关紧要的。


From a comment of mine in a previous question :

Since there can't be instances of abstract classes a pure virtual function can never be selected after overload resolution

The obvious response was :

abstract_class* a = new derived_class; a->pure_virtual_function();

and the best proof of correctness saying :

Dynamic dispatch occurs at runtime, based on whichever object is actually being used at that time. Overload resolution occurs at compile time.


Yet what bothers me is that when resolving explicitly the scope of a class member in our case compilation fails, so it looks like a pure virtual function is never actually selected through overload resolution :

struct B
{
    virtual void copy(B const& rhs) = 0;
};

struct D : B
{
    void copy(B const& rhs)
    {
        D const *pd = dynamic_cast<D const*>(&rhs);
        if (pd) {
            y = pd->y;
        }
    }
    int y;
};

int main()
{
    D d1, d2; 
    d1.y = 2;
    d2.y = 5;

    B *p1(&d1), *p2(&d2); 

    ////////////////////////////////////////////
    (*p1).B::copy(*p2);
    ////////////////////////////////////////////

    return 0;
}

Error message

undefined reference to `B::copy(B const&)'

Demo

What's the case here and if the pure virtual function is actually chosen in overload resolution why can't I "force" the compiler to do the same thing (as the resolution does).

解决方案

A pure virtual function can be selected by overload resolution. In most cases it will not be called though. Its final overrider will be called instead, just like with any other virtual function.

struct A
{
  virtual void foo() = 0;
  void foo(int);
};

A* getA();

int main ()
{
   A* a = getA();
   a->foo();
}

struct B : A
{
  void foo() {}
};
A* getA()
{
  return new B;
}

Overload resolution selects A::a() (not B::a()) at compile time, then B::a() is found and called at run-time by the virtual dispatch mechanism. Purity is irrelevant here.

这篇关于是一个纯虚函数实际上在过载分辨率中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆