Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects of the same name in base and derived class not getting flagged as an error

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?

like image 589
John Smith Avatar asked May 05 '10 00:05

John Smith


People also ask

When we use the same function name in both the base and derived class the function in base class is declared as?

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.

What happens when a reference to a derived class object is assigned to a base class reference variable?

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.

Can a derived class have pointer objects?

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.

Can we create object of base class in C#?

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.


2 Answers

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.

like image 128
James McNellis Avatar answered Sep 20 '22 17:09

James McNellis


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.

like image 38
Jonathan M Davis Avatar answered Sep 17 '22 17:09

Jonathan M Davis