CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2002
    Posts
    2

    Question overloading a function in a derived class

    Assume the following situation:

    class A
    {
    public:
    A();
    virtual ~A();

    virtual void One();
    virtual void One(int w);

    virtual void Two();
    };

    class B : public A
    {
    public:
    B();
    virtual ~B();

    void One(int w);
    void Two(int w);
    };

    The function call:

    B obj;
    obj.One();
    obj.Two();

    results in an error:
    function does not accept 0 parameter


    The compiler doesn't find the appropirate function in the
    base class A. Why??? Any ideas?
    Just copying the base class functions to the derived class could not be a good solution, coz I had to edit the code of the derived class whenever I add or change a concerning function to the base class.

    Many thanks for help.
    Andrea

    PS: I work with MS Visual C++ 6.0

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    59
    You run into the "Hiding Rule".

    "Hiding is defined as: If a derived class defines a name which
    had been defined in a class that it is inherited from, the
    derived class definition will override not overload or override
    that name.".

    In the specified class you hide the base class function when overloading the functions, i.e. One() and Two() is not defined for B.

    You could redefine the functions in the specified class

    class B : public A
    {
    public:
    B();
    virtual ~B();

    virtual void One() {A::One();};
    virtual void One(int w);

    virtual void Two() {A::Two();};
    virtual void Two(int w);
    };

    villemos.

  3. #3
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    59
    PS If your lucky, then your compiler supports the keywork "using", which "unhides" the functions. Define B as

    class B : public A
    {
    public:
    B();
    virtual ~B();

    using A::One;
    using A::Two;

    virtual void One(int w);
    virtual void Two(int w);
    };

    I have never used this, but who knows; It might work!

    villemos.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured