0

Firstly, I've read most of the topics on circular dependency here and there with no luck to my problem... so I boiled it down to a simple example, could have gone with A and B naming but this way maybe the goal's more clear.

I understand that the compiler can't know the size to allocate this way but forward declaration would leave me with an incomplete type which wouldn't work either.

My last guess would be to make a third class, but I find this over complicated ...any suggestions?

file Layer.h

#ifndef LAYER
#define LAYER
#include Shape.h

struct Layer {

    int *bufferid

    Shape *canvas = new Shape(); // here i need to know about Shape

    void draw() { canvas->draw(); }

}
#endif

file Shape.h

#ifndef SHAPE
#define SHAPE
#include Layer.h

struct Shape {

    void draw(Layer *layer) { draw(layer->buffer); } // here I need to know about Layer

    void draw(int *bufferid = nullptr) { /* if nullptr render to output else to buffer */ }

}
#endif
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ebkgne
  • 39
  • 6
  • 2
    Please provide a [mre]. For that clean up the pseudo `...` and provide a declaration of `Buffer`. Then it will be easy to answer. – Yunnosch May 07 '20 at 14:59
  • 1
    `Shape` needs only a [forward declaration](https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c) of `Layer` and `Buffer`, it doesn't need full headers. – Yksisarvinen May 07 '20 at 14:59
  • @Yksisarvinen Please turn that into an answer or help OP to improve the question to the point where you can answer. – Yunnosch May 07 '20 at 15:00
  • 1
    How about removing the `draw(Layer*)` in Shape.h and always call it with the buffer you can get at the call site? – Louis Cloete May 07 '20 at 15:00
  • I'm with Louis here. This seems like an avoidable circular reference. Just pass in what's strictly necessary to draw, not the whole `Layer`. Defaulting to `nullptr` in your `draw()` call also seems bizarre. Surely you need to draw to *something*. – tadman May 07 '20 at 15:02
  • Thx for the answers ! yunnosch I've did what u asker. Louis, I know I could do like you say, but I find it less elegant ^^ , this way i don't need to now the member name or anything, juste draw(layername) .... To Yksisarvinen , I've been trying but since I'm creating the Shape in the Layer it must know what size to allocate , or im getting something wrong here – ebkgne May 07 '20 at 15:06

1 Answers1

1

The definition of Shape does not depend on definition of Layer. It only depends on the declaration Therefore, you can simply define Shape before Layer. The definition of its member function does depend on Layer, but you can define the member function after defining Layer.

The definition of Layer wouldn't need to depend on definition of Shape either, if only you would use member initialiser list instead of a default member initialiser. But, we don't need to get rid of the default member initialiser because we can define Shape first.

So, here is a working order of definition:

  • Declare Layer
  • Define Shape
  • Define Layer
  • Define void Shape::draw(Layer *layer)

how you define the draw member afterwards ?

A member function is defined like this:

void Shape::draw(Layer *layer) { draw(layer->buffer); }

I'm refering to a Layer's member inside Shape, so how a declare would be enough ?

If you define the member function outside the definition of Shape, then you aren't referring to Layer's member inside the definition of Shape.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Hey thx ! So maybe the only thing I don't get is how you define the draw member afterwards as I am header only ? and also, but correct me if I'm wrong, I'm refering to a Layer's member inside Shape, so how a declare would be enough ? – ebkgne May 07 '20 at 15:15
  • thx again :D but i've edited my comment a bit late sorry ^^ ... "as I am header only" – ebkgne May 07 '20 at 15:19
  • @ebkgne header only is not a problem. – eerorika May 07 '20 at 15:20
  • Ok i think I get you, admiting I only have those 2 headers and a main.cpp calling them, I'd end up defining Draw in the main.cpp ? I still find that inelegant but maybe that's because I shouldn't be headers only .... ? – ebkgne May 07 '20 at 15:22
  • @ebkgne What do you mean by *"defining Draw in the main"*? – eerorika May 07 '20 at 15:23
  • well this : void Shape::draw(Layer *layer) { draw(layer->buffer); } would go in the main.cpp ? or in the layer.h after the class if that works ? – ebkgne May 07 '20 at 15:24
  • @ebkgne You can define it where-ever you want as long as it is after both definition of `Layer` and obviously `Shape`. – eerorika May 07 '20 at 15:26
  • great, I think I'm lacking of organisation. anyway I'll make it work this way. thx, I've gave u a point but i'm too low for it to appear. have a good day – ebkgne May 07 '20 at 15:30