vendredi 8 février 2019

C++ private nested class define methods in class definition or outside?

If I have a private nested class that I define in a source file(.cpp), should I define all the nested class methods in the class definition itself or after the class definition? So in the example below, should foo() and other methods be defined in the class definition or outside like below? I know that methods defined in the class definition are implicitly requested to be inline and I've read that only trivial methods should be defined in the class definition for this reason. In this case would the same guidance hold?

// OuterClass.h
class OuterClass {

private:
  // declare nested class
  class NestedClass;
  NestedClass *nestedPtr_;
};

// OuterClass.cpp
class OuterClass::NestedClass {
public:
  NestedClass() {}

  void foo();
};

OuterClass::NestedClass::foo() {

}

Aucun commentaire:

Enregistrer un commentaire