这听起来像是一个奇怪的问题,但是当我在 Visual Studio 2022 中使用以下示例代码时,IntelliSense 警告我父类中的成员
iMember
不可访问,即使代码本身对我来说似乎没问题并且正在运行没有任何问题。
示例代码:
template<class S, class T> class Child;
template<class S, class T>
class Base {
int iMember = 0;
public:
Base(const Child<S, T>& child);
Base(int iMember) : iMember(iMember) {}
void Print() { std::cout << iMember << std::endl; }
};
template<class S, class T>
class Child : public Base<S, T>{
public:
Child(int i) : Base<S,T>(i) {}
};
//the following constructor causes the IntelliSense warning that Base<S,T>::iMember is inaccessible
template<class S, class T>
Base<S, T>::Base(const Child<S, T>& child) : iMember(child.iMember) {}
int main() {
Child<int, int> cc(234);
Base<int, int> bb(cc);
cc.Print(); //prints 234
}
我不知道为什么 IntelliSense 会发出此警告,但也许我的代码有未定义的行为。
“但也许我的代码有未定义的行为......”
该程序定义良好,即给定代码中没有 UB。
以下构造函数会导致 IntelliSense 警告
不可访问Base<S,T>::iMember
在基类的成员初始值设定项列表中,您可以安全/正确地编写
child.iMember
,因为您位于基类的范围内。