Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. c++/cli define new explicit implementation of a sealed method

c++/cli define new explicit implementation of a sealed method

Scheduled Pinned Locked Moved Managed C++/CLI
c++questionregexhelp
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marius Bancila
    wrote on last edited by
    #1

    Let's say I have this code:

    interface class IFoo
    {
    public:
       void foo();
    };
    
    ref class FooBase : public IFoo
    {
    public:
       virtual void foo() sealed = IFoo::foo
       {
       }
    };
    

    I need to define a new explicit foo() in a derived class, that overrides the sealed method in the base class. How do I do that? I tried a lot of things and none compiled.

    ref class FooDerived : public FooBase
    {
    public:
       virtual void foo() 
       {
       }
    };
    

    results in error C4485: 'FooDerived::foo' : matches base ref class method 'FooBase::foo', but is not marked 'new' or 'override'; 'new' (and 'virtual') is assumed 1> .\Dlg.cpp(22) : see declaration of 'FooBase::foo' 1> Specify 'override' (and 'virtual') to override the ref class virtual method 1> Specify 'new' (and 'virtual') to hide the ref class virtual method with a new virtual method 1> Position for 'new' and 'override' keywords is after method parameter list but if I add new

    ref class FooDerived : public FooBase
    {
    public:
       virtual void foo() new
       {
       }
    };
    

    I get Dlg.cpp(30) : error C2059: syntax error : 'string' Dlg.cpp(31) : error C2091: function returns function also

    ref class FooDerived : public FooBase
    {
    public:
       virtual void foo() new = FooBase::foo
       {
       }
    };
    

    results in 1>.\Dlg.cpp(30) : error C2059: syntax error : 'string' 1>.\Dlg.cpp(30) : error C2091: function returns function 1>.\Dlg.cpp(31) : warning C4569: 'FooBase::foo' : no members match the signature of the explicit override 1>.\Dlg.cpp(31) : error C3671: 'FooDerived::foo' : function does not override 'FooBase::foo' and

    ref class FooDerived : public FooBase, public IFoo
    {
    public:
       virtual void foo() new = IFoo::foo
       {
       }
    };
    

    generates 1>.\Dlg.cpp(30) : error C2059: syntax error : 'string' 1>.\Dlg.cpp(30) : error C2091: function returns function 1>.\Dlg.cpp(31) : warning C4569: 'IFoo::foo' : no members match the signature of the explicit override 1>.\Dlg.cpp(31) : error C3671: 'FooDerived::foo' : function does not override 'IFoo::foo' What I'm trying to do is overriding

    J 1 Reply Last reply
    0
    • M Marius Bancila

      Let's say I have this code:

      interface class IFoo
      {
      public:
         void foo();
      };
      
      ref class FooBase : public IFoo
      {
      public:
         virtual void foo() sealed = IFoo::foo
         {
         }
      };
      

      I need to define a new explicit foo() in a derived class, that overrides the sealed method in the base class. How do I do that? I tried a lot of things and none compiled.

      ref class FooDerived : public FooBase
      {
      public:
         virtual void foo() 
         {
         }
      };
      

      results in error C4485: 'FooDerived::foo' : matches base ref class method 'FooBase::foo', but is not marked 'new' or 'override'; 'new' (and 'virtual') is assumed 1> .\Dlg.cpp(22) : see declaration of 'FooBase::foo' 1> Specify 'override' (and 'virtual') to override the ref class virtual method 1> Specify 'new' (and 'virtual') to hide the ref class virtual method with a new virtual method 1> Position for 'new' and 'override' keywords is after method parameter list but if I add new

      ref class FooDerived : public FooBase
      {
      public:
         virtual void foo() new
         {
         }
      };
      

      I get Dlg.cpp(30) : error C2059: syntax error : 'string' Dlg.cpp(31) : error C2091: function returns function also

      ref class FooDerived : public FooBase
      {
      public:
         virtual void foo() new = FooBase::foo
         {
         }
      };
      

      results in 1>.\Dlg.cpp(30) : error C2059: syntax error : 'string' 1>.\Dlg.cpp(30) : error C2091: function returns function 1>.\Dlg.cpp(31) : warning C4569: 'FooBase::foo' : no members match the signature of the explicit override 1>.\Dlg.cpp(31) : error C3671: 'FooDerived::foo' : function does not override 'FooBase::foo' and

      ref class FooDerived : public FooBase, public IFoo
      {
      public:
         virtual void foo() new = IFoo::foo
         {
         }
      };
      

      generates 1>.\Dlg.cpp(30) : error C2059: syntax error : 'string' 1>.\Dlg.cpp(30) : error C2091: function returns function 1>.\Dlg.cpp(31) : warning C4569: 'IFoo::foo' : no members match the signature of the explicit override 1>.\Dlg.cpp(31) : error C3671: 'FooDerived::foo' : function does not override 'IFoo::foo' What I'm trying to do is overriding

      J Offline
      J Offline
      John Schroedl
      wrote on last edited by
      #2

      You could use Renamed Overriding which will work if you're always called through an interface handle.

      interface class IFoo
      {
      public:
      void foo();
      };

      ref class FooBase : public IFoo
      {
      public:
      virtual void foo() sealed = IFoo::foo
      {
      Console::WriteLine("FooBase");
      }
      };

      ref class FooDerived : public FooBase
      {
      public:
      // derivedFoo is the implementation of IFoo::foo
      virtual void derivedFoo() = IFoo::foo
      {
      Console::WriteLine("FooDerived");
      }
      };

      int main(array ^args)
      {
      FooBase^ fb = gcnew FooBase();
      fb->foo(); // Prints "FooBase"

      FooDerived^ fd = gcnew FooDerived();
      fd->foo();					// Prints "FooBase"
      
      IFoo^ iface = dynamic\_cast(fb);
      iface->foo();					// Prints "FooBase"
      
      iface = dynamic\_cast(fd);
      iface->foo();					// Prints "FooDerived"
      
      return 0;
      

      }

      J 1 Reply Last reply
      0
      • J John Schroedl

        You could use Renamed Overriding which will work if you're always called through an interface handle.

        interface class IFoo
        {
        public:
        void foo();
        };

        ref class FooBase : public IFoo
        {
        public:
        virtual void foo() sealed = IFoo::foo
        {
        Console::WriteLine("FooBase");
        }
        };

        ref class FooDerived : public FooBase
        {
        public:
        // derivedFoo is the implementation of IFoo::foo
        virtual void derivedFoo() = IFoo::foo
        {
        Console::WriteLine("FooDerived");
        }
        };

        int main(array ^args)
        {
        FooBase^ fb = gcnew FooBase();
        fb->foo(); // Prints "FooBase"

        FooDerived^ fd = gcnew FooDerived();
        fd->foo();					// Prints "FooBase"
        
        IFoo^ iface = dynamic\_cast(fb);
        iface->foo();					// Prints "FooBase"
        
        iface = dynamic\_cast(fd);
        iface->foo();					// Prints "FooDerived"
        
        return 0;
        

        }

        J Offline
        J Offline
        John Schroedl
        wrote on last edited by
        #3

        Perhaps better. This seems to be what you're after to me.

        ref class FooDerived : public FooBase
        {
        public:
        virtual void foo() new = IFoo::foo
        {
        Console::WriteLine("FooDerived");
        }
        };

        John

        M 2 Replies Last reply
        0
        • J John Schroedl

          Perhaps better. This seems to be what you're after to me.

          ref class FooDerived : public FooBase
          {
          public:
          virtual void foo() new = IFoo::foo
          {
          Console::WriteLine("FooDerived");
          }
          };

          John

          M Offline
          M Offline
          Marius Bancila
          wrote on last edited by
          #4

          Yes. This seem to be what I was looking for. It's curious that out of all my attempts, this one I missed. :( BTW, there is a little error in your sample.

          FooDerived^ fd = gcnew FooDerived();
          fd->foo(); // Prints "FooBase"

          This obviously prints FooDerived. And the first example, where you say virtual void foo() = IFoo::foo does not compile. But thanks again for the help.

          J 1 Reply Last reply
          0
          • J John Schroedl

            Perhaps better. This seems to be what you're after to me.

            ref class FooDerived : public FooBase
            {
            public:
            virtual void foo() new = IFoo::foo
            {
            Console::WriteLine("FooDerived");
            }
            };

            John

            M Offline
            M Offline
            Marius Bancila
            wrote on last edited by
            #5

            It doesn't work in MFC apps with /clr support.

            1>.\mfc_mm_2008.cpp(38) : error C2059: syntax error : 'string'
            1>.\mfc_mm_2008.cpp(38) : error C2091: function returns function
            1>.\mfc_mm_2008.cpp(39) : warning C4569: 'IFoo::foo' : no members match the signature of the explicit override
            1>.\mfc_mm_2008.cpp(39) : error C3671: 'FooDerived::foo' : function does not override 'IFoo::foo'

            The reason is MFC is rewriting the new operator with

            #ifdef _DEBUG
            #define new DEBUG_NEW
            #endif

            So anyone wanting to do this in MFC, make sure you don't have that replacement of new before your declaration of the new function definition.

            J 2 Replies Last reply
            0
            • M Marius Bancila

              Yes. This seem to be what I was looking for. It's curious that out of all my attempts, this one I missed. :( BTW, there is a little error in your sample.

              FooDerived^ fd = gcnew FooDerived();
              fd->foo(); // Prints "FooBase"

              This obviously prints FooDerived. And the first example, where you say virtual void foo() = IFoo::foo does not compile. But thanks again for the help.

              J Offline
              J Offline
              John Schroedl
              wrote on last edited by
              #6

              Actually, that's not a error in the output, that's a direct paste from running the code and the output. There is also no compile error for me (this was directly from running code). Notice the function name in FooDerived; it's "derivedFoo()" not "foo()". FooDerived can only print "FooDerived" when fd->derivedFoo() is called OR when called via an IFoo^. So, calling fd->foo() does call the base class implementation.

              virtual void derivedFoo() = IFoo::foo

              1 Reply Last reply
              0
              • M Marius Bancila

                It doesn't work in MFC apps with /clr support.

                1>.\mfc_mm_2008.cpp(38) : error C2059: syntax error : 'string'
                1>.\mfc_mm_2008.cpp(38) : error C2091: function returns function
                1>.\mfc_mm_2008.cpp(39) : warning C4569: 'IFoo::foo' : no members match the signature of the explicit override
                1>.\mfc_mm_2008.cpp(39) : error C3671: 'FooDerived::foo' : function does not override 'IFoo::foo'

                The reason is MFC is rewriting the new operator with

                #ifdef _DEBUG
                #define new DEBUG_NEW
                #endif

                So anyone wanting to do this in MFC, make sure you don't have that replacement of new before your declaration of the new function definition.

                J Offline
                J Offline
                John Schroedl
                wrote on last edited by
                #7

                Good tip about the DEBUG_NEW screwing up the new keyword. I tried the code in my MFC + /clr application but we've had to remove those DEBUG_NEW defines for other reasons so I didn't run into this issue. Good to know! John

                1 Reply Last reply
                0
                • M Marius Bancila

                  It doesn't work in MFC apps with /clr support.

                  1>.\mfc_mm_2008.cpp(38) : error C2059: syntax error : 'string'
                  1>.\mfc_mm_2008.cpp(38) : error C2091: function returns function
                  1>.\mfc_mm_2008.cpp(39) : warning C4569: 'IFoo::foo' : no members match the signature of the explicit override
                  1>.\mfc_mm_2008.cpp(39) : error C3671: 'FooDerived::foo' : function does not override 'IFoo::foo'

                  The reason is MFC is rewriting the new operator with

                  #ifdef _DEBUG
                  #define new DEBUG_NEW
                  #endif

                  So anyone wanting to do this in MFC, make sure you don't have that replacement of new before your declaration of the new function definition.

                  J Offline
                  J Offline
                  John Schroedl
                  wrote on last edited by
                  #8

                  BTW, my kids have spent many hours playing your 'Alchemy' game so thanks for that :-)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups