Thread: Hiding member function of Derived class from object of Derived class...

  1. #1
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9

    Question Hiding member function of Derived class from object of Derived class...

    Hello,

    Code:
    class Base
    {
       public:
           void myFun(){ "I m Base" };
    };
    
    class Derived : public Base
    {
       public:
            new void myFun(){ "I m Derived" };      
            // This declaration is possible in C#.. and the similar thing i want to implement in C++.
    };
    
    int main()
    {
        Base b;
        Derived d;
      
        b.myFun();
        d.myFun();      <<--
       
        return 0;
    }
    -------------------------------------------------------------------------------------------------------------------
    Output:

    I m Base
    I m Base

    Here, you can see the Derived::myFun() is hidden from object of Derived class i.e, d...
    because i have declared myFun()....

    Code:
    new void myFun(){ .... }
    <<--

    So, how can i do this similar thing in C++..

    Thanks & Regards
    Kunal Nandi.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    myFun() is non-virtual, so declaring a member function of the same name in Derived will hide myFun() from the base class. So you could declare a private myFun() member function in the Derived class, and the name hiding will take effect.

    However, this will break the Liskov substitution principle. Why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9
    Currently i am Porting C# code to C++ (Qt).....
    In that C# code there is one declaration of above type.. and i want to do the same in C++...
    the declaration of code is as follows....

    Code:
    new public void SetCustomProperties(Hashtable properties)
    {
        .........
        ......
        ..
    }
    so, the conclusion is tht i can't change the declaration of Derived Member function from public to private.. because it is somewhere called outside the class..

    Please go through this link u will get wht excetly i want to ask.

    http://msdn2.microsoft.com/en-us/lib...w2(VS.80).aspx

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    so, the conclusion is tht i can't change the declaration of Derived Member function from public to private.. because it is somewhere called outside the class..
    In that case just declare it public. The name hiding mechanism will still work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9
    you want me to make both member funtions of Base an Derived class Virtual.. is it so..???

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    you want me to make both member funtions of Base an Derived class Virtual.. is it so..???
    That depends on your design. I am just saying that name hiding will happen if you declare a derived class member function with the same name as a base class non-virtual member function. If you use virtual functions, name hiding will not happen, but the derived class member function will override the base class member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9
    can you modify that code.. which i hv shown above.. according to ur view..

  8. #8
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9
    I m not gettting.. so please if possible then give any example code.. of name hiding.. or jst modify the code which i hv written..

    Thanks..

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    class Base
    {
       public:
           void myFun(){ std::cout << "I m Base" };
    };
    
    class Derived : public Base
    {
       public:
            void myFun(){ std::cout << "I m Derived"; };      
    };
    
    int main()
    {
        Base b;
        Derived d;
      
        b.myFun();
        d.myFun(); 
       
        return 0;
    }
    The above code will work [I fixed the myFun so that it's actually doing something].

    It would be nicer to use a virtual function, because that way you can use a pointer for the base-object, and get access to the version of the object that you actually used for the instance, e.g:
    Code:
    class Base
    {
       public:
           virtual void myFun(){ std::cout << "I m Base" };
    };
    
    class Derived : public Base
    {
       public:
            virtual void myFun(){ std::cout << "I m Derived"; };      
    };
    
    int main()
    {
        Base *b1;
        Base *b2;
        Derived d;
      
        b1 = new Base;
        b2 = new Derived;
        b1->myFun(); // Base::myFun()
        b2->myFun(); // Derived::myFun()
        d.myFun();     // Derived::myFun();
       
        return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-22-2009, 11:12 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Is it possible to have callback function as a class member?
    By Aidman in forum Windows Programming
    Replies: 11
    Last Post: 08-01-2003, 11:45 AM
  5. derived class member function pointer
    By btq in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 12:41 PM