0

I am reading some c++ code and am slightly confused by the correct interpretation of the parenthesized indirection operators. For example, in the line

b = ((aclass*)a)-> b

I cannot speed-read over that (aclass*) part, that is the parenthesised indirection operator. Why are the parentheses needed and which ambiguity do they solve?

I tend to interpret the above as an instruction to define b in the current scope by assigning it the value of the namesake member of aclass-type object referenced by the pointer a. That is, something like

b = (aclass *a)->b

Likewise, in another place there is a construction:

const aclass *a = (aclass*) b

I wonder whether I am misinterpreting the operators and their precedence, or I could do without those parentheses.


I have had a look at When do extra parentheses have an effect, other than on operator precedence?, but it sounds overly technical for me to spot an tip.

XavierStuvw
  • 1,294
  • 2
  • 15
  • 30
  • 3
    `(aclass*)` is a C-style cast, and it always a syntax of `(Type)`. `(aclass *a)` would simply be invalid in this context. What purpose does it serve? Depends on the `a`, and `aclass`. – Algirdas Preidžius Feb 20 '20 at 17:20
  • @AlgirdasPreidžius I don't know much of the purpose since this is precisely what I am trying to infer from the raw source. Your tip that it is a C-style cast, however, points me in the right direction. If you wish to (elaborate on and) convert your comment into an answer, it will be useful for future readers. – XavierStuvw Feb 20 '20 at 17:25
  • 2
    In English, treat `a` as a pointer to a `aclass` (no matter how bad an idea this might prove to be). Then dereference the pointer and access its member `b`. Finally assign the result to variable `b`. You have to do stuff like this from time to time, but it's best avoided. The c-style cast turns off the compilers brain and it does exactly what you tell it to do without any checking. If you're wrong, the compiler can't warn you so you have to be damn sure this is what you want to do. – user4581301 Feb 20 '20 at 17:26
  • @XavierStuvw If you gave a more detailed example, including e.g. the type of `a` and how (if at all) it relates to `aclass*` and including an (abbreviated) definition of `aclass` / the type of `b`, then one could say what semantically happens here. As it is right now there is not much more to say except that it is a C style cast syntactically. – walnut Feb 20 '20 at 17:30
  • @XavierStuvw No need to write an answer explaining the casts, since there are plenty of better answers on SO, already. An example of one: [When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?](https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used) – Algirdas Preidžius Feb 20 '20 at 17:32
  • @AlgirdasPreidžius @walnut admit unfamiliarity with C-style casts, so for me having pointed this out is already an answer to my wondering. Please note the tag `syntax`. I will study the subject and revisit that code in that new light. Thanks for your "pointers" – XavierStuvw Feb 20 '20 at 17:44
  • ... and in the above an 'I' before 'admit' has been accidentally deleted while editing. Apologies for the confusion – XavierStuvw Feb 20 '20 at 18:47

0 Answers0