Is there any pitfalls or negative consequences to doing something like this.
Any ways to safeguard against somebody accidentally calling an object recursively to itself?
Like if I changed line 10 to
cout << "cubea surface area: " << cubea.surfacearea(cubea) << endl; it would be a recursive loop.
It'd compile but then crash. An example like this is easy to see where it went wrong, but on something more abstracted it might be difficult to track down.
I was mostly wondering if there was a way to call objects to itself but to do it with some kind of recursive protection.
This example seems rather contrived; why not just call cubea.surfacearea() and have that method take no parameters, instead operating on this? As it is, it displayed the volume of the passed cube then returns the surface area of the calling cube, which makes very little sense to me.
In any case, I'm not sure what kind of solution you are looking for; recursion is a feature of the language, having at least as many valid uses as there are mistaken ones (such as the infinite example you gave), so my honest opinion would just be to not do it. If your code is convoluted to the point you can't follow an issue like that, perhaps it is too complex and should be simplified or broken down into pieces that you can confirm work?