문제

Regarding the following C++ program:

class Base { };

class Child : public Base { };

int main()
{   
    // Normal: using child as base is allowed
    Child *c = new Child();
    Base *b = c;

    // Double pointers: apparently can't use Child** as Base**
    Child **cc = &c;
    Base **bb = cc;

    return 0;
}

GCC produces the following error on the last assignment statement:

error: invalid conversion from ‘Child**’ to ‘Base**’

My question is in two parts:

  1. Why is there no implicit conversion from Child** to Base**?
  2. I can make this example work with a C-style cast or a reinterpret_cast. Using these casts means throwing away all type safety. Is there anything I can add to the class definitions to make these pointers cast implicitly, or at least phrase the conversion in a way that allows me to use static_cast instead?
도움이 되었습니까?

해결책

PageViewer WebPart를 사용해보십시오!그것은 다른 페이지를 (기본적으로 iFrame) 문서

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top