(*this)-> question

Hello

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.

1
2
3
4
5
6
7
8
9
10
11
12
void CSomeClass::SetSomeVal(double valu1, double valu2, double valu3)
{
    try{
          (*this)->SetSomeVal(valu1, valu2, valu3);
    }
    catch (ArgumentOutOfRangeException^ e) {
          trow SomeArgumentOutOfRangeExceptionFunction(e);
    }
    catch (Exception^ e) {
          trow SomeExceptionFunction(e);
    }
}


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.

Michael
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
Last edited on
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.
Last edited on
(*this)-> is evil

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)

Andy
Last edited on
closed account (zb0S216C)
Why is the CSomeClass::SetSomeVal( ) calling itself? Recursive call, much.

Wazzak
It's not recursive. It calls the overloaded operator -> of 'CSomeClass' which probaly calls another class. Rather ugly code though
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!)
Last edited on
closed account (1vRz3TCk)
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,
1
2
3
try{
          (*this)->SetSomeVal(valu1, valu2, valu3);
    }
is unnecessary.

Correct me if I'm wrong.
closed account (1vRz3TCk)
It is not recursion, it is redirection. How and why can not be determined from the given code.
Unless the class is some kind of pointer, it can't be too wise to overload operator->, surely.
closed account (1vRz3TCk)
That is the point; we don't know what the class is or does, so we can't say if it is good or bad.
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....
My guess

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.

Topic archived. No new replies allowed.