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. Overriding an indexer

Overriding an indexer

Scheduled Pinned Locked Moved Managed C++/CLI
csharphelpquestion
2 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.
  • E Offline
    E Offline
    Esmo2000
    wrote on last edited by
    #1

    I'm trying to override an indexer in a Dictionary but I can't figure out what the syntax is (or even if this is possible). I tried overloading the operator but it simply complains that you cannot have both a default property indexer and an operator. There seems to be a lot of examples for C#, can anyone help me out? Regards, JP

    Did I post well? Rate it! Did I post badly? Rate that too!

    N 1 Reply Last reply
    0
    • E Esmo2000

      I'm trying to override an indexer in a Dictionary but I can't figure out what the syntax is (or even if this is possible). I tried overloading the operator but it simply complains that you cannot have both a default property indexer and an operator. There seems to be a lot of examples for C#, can anyone help me out? Regards, JP

      Did I post well? Rate it! Did I post badly? Rate that too!

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Esmo2000 wrote:

      I'm trying to override an indexer in a Dictionary

      Dictionary classes indexer is not virtual. So you can't override it. If you are looking for writing virtual indexer and overriding in a derived class, here you go.

      ref class BaseClass
      {
      public:
      property int Index[int]
      {
      virtual int get(int index)
      {
      Console::WriteLine("Base class get()");
      return 0;
      }
      }
      };

      ref class DerivedClass : public BaseClass
      {
      public:
      property int Index[int]
      {
      virtual int get(int index) override
      {
      Console::WriteLine("Derived class get()");
      return 0;
      }
      }
      };

      int main()
      {
      BaseClass^ b = gcnew DerivedClass();
      int a = b->Index[10];
      return 0;
      }

      Like in standard C++, implicit overriding is not supported in C++/CLI. You have to explicitly specify it with override keyword. :)

      Navaneeth How to use google | Ask smart questions

      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