3

I am following along with a video emulating a cpu in C++. The programmer used the following and it is the first time I encounter it:

This is the code from the video:

struct Mem
{
    Byte Data[MAX_MEM];

    Byte operator[]( u32 Offset ) // <-- I don't understand this syntax
    {
    }
}

I wrote it in my own project like this:

char data[1024*64];
char fun[] ( int x ) const  // <-- Is this right?
{
  return data[x];
}

my issue is with line 1# of the example. They were able to compile but I am met with errors:

incomplete type is not allowed;
array of functions is not allowed;
a type qualifier is not allowed on a nonmember function.

What does this syntax actually do; is there an alternative way to write it?

Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    Please don't start lines of code with numbers. Instead, write a comment next to the lines you want to emphasize. – cigien Jan 03 '21 at 06:11
  • What compiler is this intended to be compiled with? – tadman Jan 03 '21 at 06:12
  • This is explained here a few times already (but I can't find it) ... – Ted Lyngmo Jan 03 '21 at 06:13
  • @tadman I am using the standard Visual Studio C++ compiler that defaults when using Desktop C++ Development – Maeximilian Jan 03 '21 at 06:16
  • @TedLyngmo I tried my best searching before I posted. I'm still unable to come across something similar. Sorry for the repost if that is the case. – Maeximilian Jan 03 '21 at 06:17
  • 2
    Is this **actual** C++, or just C? Just because VC and g++ can compile the majority of C code doesn't mean the code is valid C++. – Dai Jan 03 '21 at 06:18
  • 1
    Don't worry about it :) Ted is an expert, and is a regular on SO. If they're having difficulty finding a duplicate target, you are certainly not expected to have found one. – cigien Jan 03 '21 at 06:19
  • @Dai I am not 100% certain this is C++ but the project is made in VS and the code is a .cpp file. I am 99% sure it's C++ though. However I am still learning so I could be wrong. The references I used were using the same environment. – Maeximilian Jan 03 '21 at 06:23
  • 1
    **The code in the video is nothing like the code you've posted**. `operator[]` is a special syntax in C++, it isn't semantically equivalent to `fun[]`. – Dai Jan 03 '21 at 06:23
  • 2
    @Maeximilian I've updated your question-posting to compare your code with the code from the video you linked. – Dai Jan 03 '21 at 06:25
  • @Dai the errors were just in the snip of the code. Also I simplified it for the question. However as Milo pointed out there are better ways of showing the minimal code. Again I am just curious of what the syntax does. Thanks for your input though. – Maeximilian Jan 03 '21 at 06:26
  • 1
    @Dai thank you again for your input, I was unaware that operator[] is in fact fundamentally different from a function name. – Maeximilian Jan 03 '21 at 06:30

1 Answers1

6

Your minimal example isn't quite right; the syntax they are using in the video is very particular and you've changed some crucial pieces - they are overloading the indexing operator as a member of a class. A silly minimal example of what's going on is as follows:

struct X {
   char data[1024];
   char& operator[](int index) {
      return data[index];
   }
};

where you could later write code such as

X x;
x[5] = 'a';

where the snippet x[5] will invoke your user defined operator[] passing 5 as an argument. The important point here is that, essentially, the name of the function in the class is operator[] - and that that's just some special name related to C++'s feature of operator overloading. There's some fixed set of other operators that can be defined this way - you'll also see operator() is you want x(a,b,c,...) to be defined for an object of user-defined type X or operator+ if you want x+y to be defined. Note that operator is a keyword - its name is not interchangeable with others. Also, for the indexing operator (and a few others), such functions may only be defined within classes (though some, such as operator+ can be defined outside of classes).

There's a good general reference on overloading over at this question.

Milo Brandt
  • 435
  • 4
  • 10