0

I am working on implementing an intersection test between a sphere and a triangle.

I want to design a function to do this. The function sphere_triangle_intersection_test( ... ) should be declared as a friend function to both class sphere and class triangle, as this allows access to private members. (Thus likely to be more efficient, due to no function calls to get() or set() methods.

The function will be called quite a lot of times each loop, so it would be nice to make it an inline function.

However, inline functions, at least to my understanding, must appear (be defined) in the same file as the class declaration.

Since I am using a format of one-header-per-class, is there a way around this?

I guess one possibility would be:

  • Create a file sphere.hpp for the class sphere to reside in.
  • Create a file triangle.hpp for the class triangle to reside in.
  • Declare as friend inline [return type] the function performing the intersection test, in both classes.
  • Create a file collisiontest.impl containing the definition of the inline function to perform the test.
  • #include "sphere.hpp" in triangle.hpp.
  • At the end of triangle.hpp, `#include "collisiontest.impl".

Seems a bit dodgy though... it would be easy to forget that collisiontest.impl was included at the bottom of a file...

Does anyone have any suggestions about how I might change / improve what I've got here?

Edit

It just occurred to me I could create yet another file, shapes_INCLUDE_ME.hpp with the following content:

// shapes_INCLUDE_ME.hpp

#ifndef __shapes__include__me__hpp__
#define __shapes__include__me__hpp__ // not necessary, but good practice

    #include "sphere.hpp"
    #include "triangle.hpp"

    #include "collisiontest.impl"

#endif // __shapes__include__me__hpp__

Then you would include just the one file, might neaten things up a bit? Now there's no issue about forgetting the #include at the end of the class file.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • I believe that having one header per class is often a bad decision. – Basile Starynkevitch Sep 01 '13 at 16:53
  • Look at this this could help : http://stackoverflow.com/questions/3992980/c-inline-member-function-in-cpp-file – dzada Sep 01 '13 at 16:55
  • too___many___undersco___res... – Kerrek SB Sep 01 '13 at 16:55
  • @KerrekSB well you wouldn’t want a name conflict would you? – FreelanceConsultant Sep 01 '13 at 17:01
  • @dzada I saw that question... Since `#include` is like a copy and paste, I'm guessing my idea would work? Am I correct? – FreelanceConsultant Sep 01 '13 at 17:02
  • 1
    Yes but why doing separate file if at the end you paste everything together ? Also I think you are making things complex for a very low performance improvement (if get is const and inline I don't think there will be any overhead) And don't forget that you optimize once you re done (or if you are sure) here you are probably making things to complex. Last you could also completly put the model data elsewhere than inside sphere or triangle. These should just be logic objects, not owning the data I think – dzada Sep 01 '13 at 17:05
  • @dzada Read the question: I said it would be easy to forget you `#include`-ed another file if the `#include` was placed at the end. That might be a problem, or might not be a problem, if you came to modify something later on. – FreelanceConsultant Sep 01 '13 at 17:08
  • @dzada With a short file like this it's obvious, so you wouldn't care. – FreelanceConsultant Sep 01 '13 at 17:08
  • I agree with you, I think that or you change how you wan't to do it and OR have mother class or separate data. Or you do that. – dzada Sep 01 '13 at 17:11
  • @EdwardBird regarding Kerrek's comment, get more creative and less standard-violating. Any macro id that either leads with an underscore or *contains* two *consecutive* underscores is reserved by the implementation. Has nothing to do with the question at-hand, but often notable. – WhozCraig Sep 01 '13 at 17:14
  • @WhozCraig As yes, I forgot about that when writing this example. Just for interests sake: Usually I would have written `__shapes_include_me__HPP__` – FreelanceConsultant Sep 01 '13 at 17:16
  • 1
    As was pointed out by WhozCraig, this is also a reserved symbol. Symbols containing two consecutive underscores, or beginning with an underscore followed by a capital letter, or beginning with an underscore at global scope are reserved for use by the implementation. Consider using `INC_SHAPES_INCLUDE_ME_` instead. – IInspectable Nov 09 '13 at 23:22
  • I would make a third class (collision_test) friend of both class and put the inline function in that class... Well, I doubt that inlining would have a lot of impact so I would probably prefer to hide implementation anyway so that code would not recompile any time I change any of those classes. If inlining is really helpful, I would probably use some trick like having an extra file to avoid compile-time dependencies in DEBUG. – Phil1970 Jul 12 '16 at 01:28

0 Answers0