关于C ++中虚函数的访谈问题

时间:2022-09-08 16:54:58

I was asked this crazy question. I was out of my wits.

我被问到这个疯狂的问题。我的斗智斗勇。

Can a method in base class which is declared as virtual be called using the base class pointer which is pointing to a derived class object?

可以使用指向派生类对象的基类指针调用声明为virtual的基类中的方法吗?

Is this possible?

这可能吗?

8 个解决方案

#1


50  

If you're trying to invoke a virtual method from the base class pointer, yes.

如果您尝试从基类指针调用虚方法,是的。

That's polymorphism.

If you're asking, with a base class pointer to a derived class, can you invoke a base class method that is overriden by the derived class? Yes that's also possible by explicitly scoping the base class name:

如果您要求使用基类指向派生类的指针,您是否可以调用派生类重写的基类方法?是的,通过显式确定基类名称也可以:

basePtr->BaseClass::myMethod();

#2


16  

Try:

class A            { virtual void foo(); }
class B : public A { virtual void foo(); }

A *b = new B();
b->A::foo ();

#3


10  

You mean something like this. (Where pBase is of type pointer-to-base but the pointed-to object is actually of type Derived which is derived from Base.)

你的意思是这样的。 (其中pBase是指向base的类型,但指向的对象实际上是Derived类型,它是从Base派生的。)

pBase->Base::method();

Yes, it's possible.

是的,这是可能的。

#4


10  

Yes -- you have to specify the full name though:

是 - 您必须指定全名:

#include <iostream>

struct base { 
    virtual void print() { std::cout << "Base"; }
};

struct derived : base {
    virtual void print() { std::cout << "Derived"; }
};

int main() { 
    base *b = new derived;
    b->base::print();
    delete b;
    return 0;
}

#5


5  

If I understand the question correctly, you have

如果我正确理解了这个问题,你就有了

class B 
{
public: 
    virtual void foo();
};

class D: public B
{
public:
    virtual void foo();
}

B* b = new D;

And the question is, can you call B::foo(). The answer is yes, using

问题是,你可以调用B :: foo()。答案是肯定的,使用

b->B::foo()

#6


0  

class B 
{
public: 
    virtual void foo();
};

class D: public B
{
public:
    virtual void foo();
}

B* b = new D;

Try calling

(*b).foo()

to invoke base class foo function

调用基类foo函数

#7


0  

class B { 
    public: virtual void foo(); 
};

class D: public B { 
    public: virtual void foo() 
    { 
        B::foo();
    }; 
}

B* b = new D;

Solutions :

  1. b->foo();
  2. b->B::foo()

  3. OR do not override/define foo() in the derived class D and call b->foo()

    或者不要在派生类D中覆盖/定义foo()并调用b-> foo()

  4. B objb = *b; objb.foo() ; // this is object slicing and not (*b).foo() as in one of the previous answers

    B objb = * b; objb.foo(); //这是对象切片而不是(* b).foo(),就像之前的答案之一一样

#8


-1  

No. Not in a clean way. But yes. You have to do some pointer manipulation, obtain a pointer to the vtable and make a call. but that is not exactly a pointer to base class, but some smart pointer manipulation. Another approach is using scope resolution operator on base class.

不,不是一个干净的方式。但是,是的。你必须做一些指针操作,获得一个指向vtable的指针并进行调用。但这并不是指向基类的指针,而是一些智能指针操作。另一种方法是在基类上使用范围解析运算符。

#1


50  

If you're trying to invoke a virtual method from the base class pointer, yes.

如果您尝试从基类指针调用虚方法,是的。

That's polymorphism.

If you're asking, with a base class pointer to a derived class, can you invoke a base class method that is overriden by the derived class? Yes that's also possible by explicitly scoping the base class name:

如果您要求使用基类指向派生类的指针,您是否可以调用派生类重写的基类方法?是的,通过显式确定基类名称也可以:

basePtr->BaseClass::myMethod();

#2


16  

Try:

class A            { virtual void foo(); }
class B : public A { virtual void foo(); }

A *b = new B();
b->A::foo ();

#3


10  

You mean something like this. (Where pBase is of type pointer-to-base but the pointed-to object is actually of type Derived which is derived from Base.)

你的意思是这样的。 (其中pBase是指向base的类型,但指向的对象实际上是Derived类型,它是从Base派生的。)

pBase->Base::method();

Yes, it's possible.

是的,这是可能的。

#4


10  

Yes -- you have to specify the full name though:

是 - 您必须指定全名:

#include <iostream>

struct base { 
    virtual void print() { std::cout << "Base"; }
};

struct derived : base {
    virtual void print() { std::cout << "Derived"; }
};

int main() { 
    base *b = new derived;
    b->base::print();
    delete b;
    return 0;
}

#5


5  

If I understand the question correctly, you have

如果我正确理解了这个问题,你就有了

class B 
{
public: 
    virtual void foo();
};

class D: public B
{
public:
    virtual void foo();
}

B* b = new D;

And the question is, can you call B::foo(). The answer is yes, using

问题是,你可以调用B :: foo()。答案是肯定的,使用

b->B::foo()

#6


0  

class B 
{
public: 
    virtual void foo();
};

class D: public B
{
public:
    virtual void foo();
}

B* b = new D;

Try calling

(*b).foo()

to invoke base class foo function

调用基类foo函数

#7


0  

class B { 
    public: virtual void foo(); 
};

class D: public B { 
    public: virtual void foo() 
    { 
        B::foo();
    }; 
}

B* b = new D;

Solutions :

  1. b->foo();
  2. b->B::foo()

  3. OR do not override/define foo() in the derived class D and call b->foo()

    或者不要在派生类D中覆盖/定义foo()并调用b-> foo()

  4. B objb = *b; objb.foo() ; // this is object slicing and not (*b).foo() as in one of the previous answers

    B objb = * b; objb.foo(); //这是对象切片而不是(* b).foo(),就像之前的答案之一一样

#8


-1  

No. Not in a clean way. But yes. You have to do some pointer manipulation, obtain a pointer to the vtable and make a call. but that is not exactly a pointer to base class, but some smart pointer manipulation. Another approach is using scope resolution operator on base class.

不,不是一个干净的方式。但是,是的。你必须做一些指针操作,获得一个指向vtable的指针并进行调用。但这并不是指向基类的指针,而是一些智能指针操作。另一种方法是在基类上使用范围解析运算符。