问题描述
class Base
{
public:
type1 m_Pants;
};
class Derived : Base
{
public:
type2 m_Pants
};
这本质上没有被标记为错误,而是在创建各种各样的clobbering和问题一个项目。
This essentially didn't get flagged as an error, but was creating all kinds of clobbering and issues throughout a project.
有没有人知道不会标记这个技术?
Does anyone know of a technicality that wouldn't flag this?
推荐答案
它不会被标记为错误,因为它不是一个错误。没有什么说你不能在派生类中有成员与基类中的成员命名相同。
It isn't getting flagged as an error because it's not an error. There's nothing that says you can't have members in a derived class that are named the same as members in a base class.
如果你有一个对象<$ c $类型 Derived
的c> obj ,则 obj.m_Pants
是指 m_Pants
在派生
中。如果你想引用基本成员,你可以使用 obj.Base :: m_Pants
。
If you have an object obj
of type Derived
, then obj.m_Pants
refers to the m_Pants
in Derived
. If you want to refer to the base member, you can do so using obj.Base::m_Pants
.
如果您在 Base
的成员函数中或有 Base * code>指向
Derived
类型的对象,则 m_Pants
总是指 Base
,因为在这些上下文中不知道类 Derived
及其成员。
If you are in a member function of Base
or have a Base*
that points to an object of type Derived
, then m_Pants
always refers to the member of Base
, because in those contexts there is no knowledge of the class Derived
and its members.
好吧,它不是代码错误;它几乎肯定是一个设计错误。
Well, it's not a code error; it's almost certainly a design error.
这篇关于基类和派生类中相同名称的对象未标记为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!