-1

i am new to c++ and i know so much more python than c++ and i have to change a code from c++ to python, in the code to change i found this sentence:

p->arity = std::stoi(x, nullptr, 10);

i think for sake of simplicity we can use

p->arity = x;   /* or some whit pointers im really noob on c++ but i think this is not importan */

in python does this means something like

p[arity] = x

or similar? or what? im a little lost whit all these new(for me) concepts like pointers and memory stuff but now whit -> operator

thanks in advance

mimus
  • 367
  • 4
  • 21
  • 2
    Nothing can be a substitute for a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – PaulMcKenzie Jan 27 '22 at 02:59
  • 5
    Don't translate code from one language to another. You will get substandard code [if not complete gibberish](https://en.wikipedia.org/wiki/All_your_base_are_belong_to_us). You need to understand the source code well enough to know what all of the observable behaviours will be. Then you write a whole new program that produces identical observable behaviour in the new language. Finally you exhaustively test the output of the new program against the output of the old program for the same inputs to ensure the observable behaviour is identical. – user4581301 Jan 27 '22 at 03:01
  • PaulMcKenzie I fully understand the advantage of reading a good book with a captivating author, I gladly take the references, although any hint to speed up the resolution of this problem is also appreciated – mimus Jan 27 '22 at 03:03
  • gibberish I'm just trying to do that, but I don't really understand what the arrow operator actually means, what is it used for? or how is it used? – mimus Jan 27 '22 at 03:05
  • 1
    Problem is, you haven't provided enough information, so people have to guess at context you haven't given. In C++, there is a common meaning of the arrow operator (`p->arity` means that `p` is a pointer to a data structure, and `p->arity` references a member named `arity` of that structure, and is equivalent to `(*p).arity`) and several valid but less obvious meanings (e.g. `p` may be an instance of a user-supplied class with an `operator->()` and several possibilities for what that operator does). There are several possible ways that *each* possibility (in C++) might be mapped to Python. – Peter Jan 27 '22 at 03:30
  • nice to read more and more about this information, the reason of the question was exactly what you pointed i do not have to much information about the context now maybe whit your answer and the orthers i can add that information once i am aware that i dont have it, what should i add to pursue this task Peter? – mimus Jan 27 '22 at 03:44
  • Try providing a small but complete sample of code, including declaration of `p` (what type is it?), how is it initialised, etc. The "complete" bit is important, because (say) if you're using a third-party library that people aren't familiar with, people still won't be able to help. Your example needs to be boiled down to the point of using the core language and standard types (e.g. in the C++ standard library, not relying on knowledge of headers for third-party libraries that may not be well known) – Peter Jan 27 '22 at 03:57
  • 1
    @mimus -- *although any hint to speed up the resolution of this problem is also appreciated* -- C++ doesn't work that way -- it isn't a computer language where you can have a cheat sheet in your pocket to look up things. C++ is one of the most difficult languages to learn. A tutorial on pointers is what a good book is for, and is way beyond a simple answer posted in the answer section. Then you have to ask yourself -- what's next you will not understand that also can be explained by actually learning C++? With C++, you can't simply cherry-pick things here and there. – PaulMcKenzie Jan 27 '22 at 04:08
  • @PaulMcKenzie i found your coment very inspiring, i like books, i prefer it agains videos or courses. but thats a nice way to see the progress, whats next? i dident know that cherry-picking was not so simple on c++, but i keep that question in mind whats the next to have a better understand c++ code that i use. thanks and any recomendations will be grateful – mimus Jan 27 '22 at 04:46
  • @peter i will try to ask for that info and try to update the post, i cant promess it 'cause i dident have the info by hand, but i try so but so far i feel very satisfied by the comunity help, i guess i can work whit this info and for sure later i will need to understand what you asked in order to end the task i was commanded, thanks a lot! – mimus Jan 27 '22 at 04:51

2 Answers2

1

What I understand about the arrow operator from this post:

An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union.

Since Python doesn't deal with pointers, this is not directly comparable.

However, you can think of its usage as similar to the dot operator in Python.

class Random:
    arity = 0

p = Random()
p.arity = x
Siddhartha
  • 311
  • 3
  • 8
  • ooooh nice!!! i supose i shoud use it carefully but i think i get it! now the code in c++ has more meaning for me, thanks! – mimus Jan 27 '22 at 03:08
  • 1
    Defining a class attribute with `arity = 0` is not needed. When you do `p.arity = x` it's shadowed by the instance attribute `arity` anyway. – Matthias Jan 27 '22 at 06:52
1

To access a member b in object a in C++, the syntax is a.b

If c is a pointer to a instead of being a itself, then the syntax is c->a, equivalent to (*a).b

There are no pointers to objects in python so it would just likely be a.b

Xascoria Dung
  • 75
  • 1
  • 9