有人可以解释为什么下面的代码不会对错误编译:C++自动“类型转换的转换
Error 1 error C2243: 'type cast' : conversion from 'Der *' to 'Base *' exists, but is inaccessible d:\users\lzizva\documents\mta\c++\projects\temp1\17022014.cpp 50 1 temp1
Error 2 error C2243: 'type cast' : conversion from 'Der *' to 'Base *' exists, but is inaccessible d:\users\lzizva\documents\mta\c++\projects\temp1\17022014.cpp 51 1 temp1
3 IntelliSense: conversion to inaccessible base class "Base" is not allowed d:\users\lzizva\documents\mta\c++\projects\temp1\17022014.cpp 50 12 temp1
4 IntelliSense: conversion to inaccessible base class "Base" is not allowed d:\users\lzizva\documents\mta\c++\projects\temp1\17022014.cpp 51 14 temp1
我认为,当有私有继承的孩子得到所有的属性和方法,并将其设置为私有它只会影响孩子的子类。 我在这里错过了什么? 编译器实际上做了什么?
由于提前, Liron
#include <iostream>
using namespace std;
class Base
{
int n;
Base* next;
public:
Base(int n, Base* next = NULL) : n(n), next(next)
{}
virtual void print() const
{
cout << n << endl;
if (next != NULL)
{
next->print();
}
}
virtual ~Base()
{
cout << "Base" << endl;
}
};
class Der : private Base
{
int counter;
public:
Der(int n, Base* next = NULL) : Base(n, next), counter(n){}
void print() const
{
cout << counter << endl;
Base::print();
}
~Der()
{
cout << "Der" << endl;
}
};
void main()
{
Der one(1);
Der two(2, &one);
Der three(3, &two);
three.print();
}