"new" modifier explicitly hide a member inherited from a base class in C#
Hello,
"new" modifier explicitly hide a member inherited from a base class in C#..
Is there any alternative in C++ which can be used like this..
as shown below..
Code:
public class MyBaseC
{
public int x;
public void Invoke() {}
}
Declaring a member with the name
Invoke
in a derived class will hide the method
Invoke
in the base class, that is:
Code:
public class MyDerivedC : MyBaseC
{
new public void Invoke() {}
}
here member function Invoke() in class MyDerivedC is hidden from the base MyBaseC...
-->> how can i implement this similar thing in C++...
Regards,
Kunal nandi.
Re: "new" modifier explicitly hide a member inherited from a base class in C#
There are many ways. The simplest is to prefix all member variables, for example "m_invoke" instead of "invoke". Another is to use a separate internal namespace:
Code:
class x {
public:
//...
void invoke() { xpriv::invoke++; }
private:
namespace xpriv {
int invoke;
}
};
You can also use a data-pointer and hide all member variables in a separate object.
Re: "new" modifier explicitly hide a member inherited from a base class in C#
Quote:
Originally Posted by
kunalnandi
here member function Invoke() in class MyDerivedC is hidden from the base MyBaseC...
-->> how can i implement this similar thing in C++...
What do you mean exactly by "hidden from the base MyBaseC"? But it seems that you just want the default behaviour of non-virtual methods:
Code:
#include <iostream>
class Base
{
public:
void foo() { std::cout<< "Base::foo" << std::endl; }
virtual void bar() { std::cout<< "Base::bar" << std::endl; }
};
class Derived : public Base
{
public:
void foo() { std::cout<< "Derived::foo" << std::endl; }
virtual void bar() { std::cout<< "Derived::bar" << std::endl; }
};
int main()
{
Derived d;
Base *b = &d;
d.foo();
b->foo(); // <--
d.bar();
b->bar();
return 0;
}
Output:
Quote:
Derived::foo
Base::foo
Derived::bar
Derived::bar
Re: "new" modifier explicitly hide a member inherited from a base class in C#
Quote:
Originally Posted by
jacek
What do you mean exactly by "hidden from the base MyBaseC"? But it seems that you just want the default behaviour of non-virtual methods:
Code:
#include <iostream>
class Base
{
public:
void foo() { std::cout<< "Base::foo" << std::endl; }
virtual void bar() { std::cout<< "Base::bar" << std::endl; }
};
class Derived : public Base
{
public:
void foo() { std::cout<< "Derived::foo" << std::endl; }
virtual void bar() { std::cout<< "Derived::bar" << std::endl; }
};
int main()
{
Derived d;
Base *b = &d;
d.foo();
b->foo(); // <--
d.bar();
b->bar();
return 0;
}
Output:
Hello,
d->bar();
this will call the member function of Derived class...
but, wht is want is.. member function of Derived class Derived::bar() should be hidden from the derived class object.... and the object of Derived class should called Base::bar() member function...
this is possible in C# if we declare the derived class member function like as follow...
// In C#
class Base
{
public:
void foo() { std::cout<< "Base::foo" << std::endl; }
virtual void bar() { std::cout<< "Base::bar" << std::endl; }
};
class Derived : public Base
{
public:
void foo() { std::cout<< "Derived::foo" << std::endl; }
new void bar() { std::cout<< "Derived::bar" << std::endl; } <---
};
int main()
{
Derived d;
d.bar(); <---
return 0;
}
Output:
Base::bar();
Re: "new" modifier explicitly hide a member inherited from a base class in C#
Quote:
Originally Posted by
kunalnandi
member function of Derived class Derived::bar() should be hidden from the derived class object.... and the object of Derived class should called Base::bar() member function...
Are you talking about this?
Code:
#include <iostream>
class Base
{
public:
void foo() { std::cout << "Base::foo" << std::endl; }
void bar() { std::cout << "Base::bar" << std::endl; }
};
class Derived : public Base
{
public:
void foo() { std::cout << "Derived::foo" << std::endl; }
};
int main()
{
Derived d;
d.foo();
d.bar(); // <--
return 0;
}
Output:
Quote:
Derived::foo()
Base::bar()