Yes, you can declare it into the class and define it into another translation unit (or same unit) like the following
main.cpp
#include <iostream>
using namespace std;
class myClass {
public:
int operator[](int idx);
};
// Definition outside of the class
int myClass::operator[](int idx) {
return 42;
}
int main() {
myClass obj;
cout << obj[55];
return 0;
}
Or something like the following
main.cpp
#include <iostream>
#include "header.h" <--
using namespace std;
int main() {
myClass obj;
cout << obj[55];
return 0;
}
header.h
#pragma once
class myClass {
public:
int operator[](int idx);
};
header.cpp
#include "header.h"
// Definition outside of the class
int myClass::operator[](int idx) {
return 42;
}
Just remember the ODR - One Definition Rule: in any translation unit a template, type, object or function shall have no more than one definition.