CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: overwrite virtual template methods

    I agree that qualifying the full name is the solution.
    But I'm morw with kempofighter on this.
    Quote Originally Posted by itcmelo
    Code:
    int main()
    {
      B b;
      int i = 10;
      bool x = b.IsValid(i); //Error, only IsValid(int, int) from B is visible.
      
      /* ... */
    }
    call to the b.IsValid was an error even before it made
    because it was called on the object, not through a reference type.
    so in this case,
    my understanding is that it was the vTable lookup mechanism that called b.IsValid(i) invalid,
    not the name hiding.
    I believe Paul was right on this when he showed the comeau version of the error
    Code:
    #include <iostream>
    
    template <typename T>
    struct Base
    {
        Base() : x(T()) {}
        virtual ~Base(){}
    
        virtual void call(const T y) const
        {
            std::cout << x + y;
        }
        virtual void call() const
        {
            std::cout << x;
        }
    protected:
        T x;
    };
    
    struct Derived : public Base<int>
    {
        virtual void call(const int y) const
        {
            std::cout << x + y;
        }
    
    };
    
    int main()
    {
        Derived d;
        Base<int>& ref = d;
        ref.call(); // valid with a warning
    }
    Here, we're not using the using declaration.
    I looked up Scott Meyer's effective C++,
    and under "Never redefine an inherited non-virtual function",
    he's also using reference types to illustrate the behavior
    If name hiding does occur before name look up happens in virtual overload resolution,
    (which I believe should happen afterwards)
    then like kempofighter, I'm lost as to why.
    Last edited by potatoCode; November 3rd, 2009 at 02:13 PM. Reason: fixed some typo

  2. #17
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: overwrite virtual template methods

    Quote Originally Posted by potatoCode
    call to the b.IsValid was an error even before it made
    because it was called on the object, not through a reference type.
    so in this case,
    my understanding is that it was the vTable lookup mechanism that called b.IsValid(i) invalid,
    not the name hiding.
    No, it is the name hiding. If not for the effects of name hiding, there should be no problem since the derived class inherits the base class members. That the call is non-virtual is not important.

    Quote Originally Posted by potatoCode
    If name hiding does occur before name look up happens in virtual overload resolution,
    (which I believe should happen afterwards)
    then like kempofighter, I'm lost as to why.
    If name hiding should occur at that point, then it should result in an error, not a warning. The warning is to help the programmer identify a probable problem, which it does in this case.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #18
    Join Date
    Sep 2005
    Posts
    11

    Re: overwrite virtual template methods

    Hi all,
    I think I found the explanation. According to the standard, if you have methods of the same name with a different signature, you cannot overwrite only one. Instead you MUST implement them all.
    Otherwise the remaining methods will be hidden.
    I think, this is one of the issues that will be changed with the next standard (coming 2010 I think) . (For other languages this is no problem either.)
    But so long - as often - it seems that it depends on the compiler you're using (and how old it is...)

    chabba

Page 2 of 2 FirstFirst 12

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