I came over a strange function at my new job today. From what it looks like to me it is a function calling itself until it trows an exception. I have asked around here at work but no one seems to be able to explain what the code is suppouse to do other than setting some values. One theory someone came up with was that it might be comunication between c++ and c# code (which I have never done or seen before). This might be the case as I have a similar c# project with the same functions in the same solution.
I cant debug the code because my version of the project is unable to build yett. It would be really great if someone has any knowlage of what the intention behind this code might be.
However, the syntax used is incorrect and unnecessary. this is a pointer to the instance of the class from which the function was called. So *this is the instance itself.
The syntax (*this)-> attempts to dereference the instance, not the pointer. This is only applicable when your class is some pointer-like type which overrides the -> (pointer to member) operator.
You need either (*this).SetSomeVal(valu1, valu2, valu3) or this->SetSomeVal(valu1, valu2, valu3). The second notation is more frequently used AFAIK.
However, the use of the this pointer in this context is not necessary anyway. If you call a class member function from within another class member function, you don't need to call it via a class instance: this->SetSomeVal(valu1, valu2, valu3); becomes this: [/code]SetSomeVal(valu1, valu2,valu3);[/code].
Ignore; I didn't read the question too closely... sorry
If that's the whole body of the function and there's no polymorphism involved, I can confidently say that the function does nothing useful at all.
Otherwise, it depends.
Obviously, if there's more code in the function, I can say nothing about it.
The (*this)-> notation makes me suspect there might be some polymorphism at work. Maybe a very bad case of abuse of recursion to iterate over a linked list?
By the way, that's not C++. That's C++/CLI; .NET and C++'s bastard child.
The only way that would compile is if the operator-> is mis-defined for that class!
this->Method() is OK to call a method, though I would expect just Method(). The use of this-> is either a work around of a scoping issue or a bad habit!
The one place to expect to see anything of the form (*ptr)->Function() is when iterating STL container with object pointers in it (but never this)
What is particularly bad about (*this)->SetSomeValue(); is that operator-> is being used for a non pointer. So someone has altered the semantics of the operator.
(Clarrification : as this is a pointer, *this is an object instance, so its methods should be called using operator. e.g. (*this).SetSomeValue();)
Andy
P.S. I have been bitten badly (i.e. wasted time debugging) by bizarre overloads twice: someone defining operator< to get their sort order right as they didn't understand algorithms; and someone who defined bool operator(const char*, const char*) so they could test char* values like
1 2 3
if( "this" == "a really bad mistake")
{
....
and broke all the standard pointer tests which were using the normal == behaviour (as we mostly used std::string, this was not as immediately obvious as it might have been!)
Without seeing the rest of the code it is supposition as to what the code is or how good or bad it is. It looks like some sort of a proxy or smart pointer.
Without seeing the rest of the code it is supposition as to what the code is or how good or bad it is.
With respect, I'd like to say that this is indeed bad code even if I don't see other parts, for the reasons:
1, This is not recursive call, simply because there is no stop condition.
2, The only possibility is that polymorphism is intended. Given that,
recursive brought to null by *this calling for one or three values to be choose, or just a null and statement breaks before crash allowing save option, is it graphical or game coding if so would be a normal procedure.
P.S. I have been bitten badly (i.e. wasted time debugging) by bizarre overloads twice: someone defining operator< to get their sort order right as they didn't understand algorithms; and someone who defined bool operator(const char*, const char*) so they could test char* values like
C++ operator overloading is a good feature but it can be "abused" to do all sorts only limited by each developer imagination. The reason Java does not have operator overloading maybe a good design decision hmmm....
CSomeClass contains a pointer/handle to some object, this is returned by the overloaded operator->(). For some reason we don't like the exceptions thrown by this object, so we want to translate them. The point of the class is to therefore to hide the pointer/handle to the original object, along with the unwanted exceptions it throws and instead provide a new object which throws the exceptions we want.