Results 1 to 5 of 5

Thread: "new" modifier explicitly hide a member inherited from a base class in C#

  1. #1
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question "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..

    Qt Code:
    1. public class MyBaseC
    2. {
    3. public int x;
    4. public void Invoke() {}
    5. }
    To copy to clipboard, switch view to plain text mode 
    Declaring a member with the name
    Invoke
    in a derived class will hide the method
    Invoke
    in the base class, that is:

    Qt Code:
    1. public class MyDerivedC : MyBaseC
    2. {
    3. new public void Invoke() {}
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jpn; 17th March 2008 at 20:31. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:
    Qt Code:
    1. class x {
    2. public:
    3. //...
    4. void invoke() { xpriv::invoke++; }
    5. private:
    6. namespace xpriv {
    7. int invoke;
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    You can also use a data-pointer and hide all member variables in a separate object.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "new" modifier explicitly hide a member inherited from a base class in C#

    Quote Originally Posted by kunalnandi View Post
    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:
    Qt Code:
    1. #include <iostream>
    2.  
    3. class Base
    4. {
    5. public:
    6. void foo() { std::cout<< "Base::foo" << std::endl; }
    7. virtual void bar() { std::cout<< "Base::bar" << std::endl; }
    8. };
    9.  
    10. class Derived : public Base
    11. {
    12. public:
    13. void foo() { std::cout<< "Derived::foo" << std::endl; }
    14. virtual void bar() { std::cout<< "Derived::bar" << std::endl; }
    15. };
    16.  
    17. int main()
    18. {
    19. Derived d;
    20. Base *b = &d;
    21.  
    22. d.foo();
    23. b->foo(); // <--
    24.  
    25. d.bar();
    26. b->bar();
    27.  
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: "new" modifier explicitly hide a member inherited from a base class in C#

    Quote Originally Posted by jacek View Post
    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:
    Qt Code:
    1. #include <iostream>
    2.  
    3. class Base
    4. {
    5. public:
    6. void foo() { std::cout<< "Base::foo" << std::endl; }
    7. virtual void bar() { std::cout<< "Base::bar" << std::endl; }
    8. };
    9.  
    10. class Derived : public Base
    11. {
    12. public:
    13. void foo() { std::cout<< "Derived::foo" << std::endl; }
    14. virtual void bar() { std::cout<< "Derived::bar" << std::endl; }
    15. };
    16.  
    17. int main()
    18. {
    19. Derived d;
    20. Base *b = &d;
    21.  
    22. d.foo();
    23. b->foo(); // <--
    24.  
    25. d.bar();
    26. b->bar();
    27.  
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "new" modifier explicitly hide a member inherited from a base class in C#

    Quote Originally Posted by kunalnandi View Post
    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?
    Qt Code:
    1. #include <iostream>
    2.  
    3. class Base
    4. {
    5. public:
    6. void foo() { std::cout << "Base::foo" << std::endl; }
    7. void bar() { std::cout << "Base::bar" << std::endl; }
    8. };
    9.  
    10. class Derived : public Base
    11. {
    12. public:
    13. void foo() { std::cout << "Derived::foo" << std::endl; }
    14. };
    15.  
    16. int main()
    17. {
    18. Derived d;
    19.  
    20. d.foo();
    21. d.bar(); // <--
    22.  
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Derived::foo()
    Base::bar()

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.