I have a function that draws a circle using OpenGL, I would like to pass it a structure containing the x and y coordinates and the radius. The problem is this same function has to be used with 3 different structures all containing the coordinates, radius and some other things that the draw function doesn’t use.
Is there some way to only have one argument for 3 different structures (only one is sent at a time).
I hope I’ve been enough precise.
PS : the functions have to be “abstract”.
Yes you can use a prototype like this:
Use the type to tell the function which struct to use the data as, and you’re good.
Second option, if you want to avoid a switch/case, and be able to add more struct types afterwards, without evolving foo, you can make sure all your structs begin with the necessary data, always in the same order, with the same type. This way you can make something like an “interface” from C++, and use abstract versions of the type:
The second part of the second method allows you more flexibility, breaking down specific information in a subtype, enables you to put the
abspart anywhere inside the struct a/b/c, and is better in the principle of a single purpose for a struct (Having coordinates and radius and other things inside a struct is not always the best.)