class Base
{
public:
type1 m_Pants;
};
class Derived : Base
{
public:
type2 m_Pants
};
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?
Overloading takes place within the same class. Overriding occurs in a base class and its derived class. Class can have any number of the overloaded functions. Can have only one overridden function per derived class.
Moreover, Object slicing happens when a derived class object is assigned to a base class object, and additional attributes of a derived class object are sliced off to form the base class object.
Explanation: A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer because object slicing happens when a derived class object is assigned to a base class object.
In C#, both the base class and the derived class can have their own constructor. The constructor of a base class used to instantiate the objects of the base class and the constructor of the derived class used to instantiate the object of the derived class.
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.
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
.
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.
One variable is shadowing the other. It's just like if you declared a member variable name x
and then had a member function which declared its own x
, except that here, one of the variables is in the base class and one is in a derived class.
int func(int x)
{
return x;
}
would return the value of x
you passed in, not that of the member variable x
. One variable "shadows" the other. It's why it's a good idea to name your variables so that they don't ever have names which could clash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With