PDA

View Full Version : "new" modifier explicitly hide a member inherited from a base class in C#



kunalnandi
17th March 2008, 12:06
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..


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:


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.

wysota
17th March 2008, 13:35
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:

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.

jacek
18th March 2008, 00:49
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:

#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:

Derived::foo
Base::foo
Derived::bar
Derived::bar

kunalnandi
19th March 2008, 05:30
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:

#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();

jacek
19th March 2008, 18:47
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?

#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:
Derived::foo()
Base::bar()