dimanche 23 décembre 2018

Can a class have a member that is another with the same name in the same namespace?

Suppose I have two classes with the same name in the same namespace in two different files.

This is so I can construct another object with each of the two classes, following the same interface but with some functions that behave differently.

For the differently behaving functions, I will redefine them in one instance of the class.

For the functions behaving the same way, I want to construct an instance of the other class and forward calls.

Is there a way to do this? Clearly I can't have two classes in the same namespace, but perhaps I can redefine the namespace/classname of the class I want to be a member in order to forward calls?

For example:

//file_1.h
namespace x {
 class y {
 }
}

//file_2.h
#include "file_1.h"
namespace x {
  class y {
    // member of same class in the other file
    y memberName;
  }
}

Aucun commentaire:

Enregistrer un commentaire