0

Assume I have a base class Shape, and a derived class from Shape, Triangle, defined in a project X in Visual Studio. I also have a class Transformations in a project Y in which I want to be able to call dynamically the appropriate method:

void Rotate(Shape& shape);
void Rotate(Triangle& triangle);

I'm trying to do this using a visitor pattern following this.

In both Shape.cpp and Triangle.cpp, I implemented to following method:

void rotate(Transformations trans) {
    trans.Rotate(this);
}

However, project X is compiled before project Y, and project Y depends on project X, which leaves me with circular dependencies.

Unfortunately, forward declaring the Transformations class is not enough, as the knowledge of its member functions is necessary.

The answers provided in this thread did not help in that regard.

Community
  • 1
  • 1
Dooggy
  • 330
  • 2
  • 11
  • 1
    You're not using references i.e. you're copying the shapes, that's why the size is required (and slicing is why it won't work anyways). Use references: `void Rotate(Shape& shape); ...(Triangle& triangle);` and forward declaration and the problem disappears. – BeyelerStudios Jun 15 '16 at 07:16
  • @BeyelerStudios you're right, but references won't solve the circular dependency problem : you can already declare a function with an incomplete type as parameter or return type, and the implementation requires the complete type to call its functions. – Quentin Jun 15 '16 at 07:55
  • @Quentin Maybe I'm missing your point, but the complete type only requires declared member functions, not the implementations of them (`[C++11: 9.2/2]`: *A class is considered a completely-defined object type (3.9) (or complete type) at the closing `}` of the class-specifier.*) You're allowed to call a function that has been declared. – BeyelerStudios Jun 15 '16 at 08:43
  • @BeyelerStudios I'm aware of this, but for example `trans.Rotate(this);` requires `Transformation` to be a complete type, whether or not you passed it by reference. – Quentin Jun 15 '16 at 08:46
  • So I don't get your point then: OP does specify, he implements his functions in source files, i.e. separate from his headers, so I assumed everything is cushty and the only thing breaking that is pass-by-value as that specifically requires the knowledge of the complete type at the point of declaration (while a reference (pointer) can deal with forward declared types). – BeyelerStudios Jun 15 '16 at 08:56
  • This does not solve the problem. The call trans.Rotate(this) requires the compiler to know about the member functions of Transformations. The link provided by the person who closed this thread does not answer that either. – Dooggy Jun 17 '16 at 07:47

0 Answers0