C++ language standard for pointer

Asked

Viewed 95 times

2

I’m starting studies on C++ would like what is the language recommendation (market standard) to reference a pointer.

ex:

l->propriedade;

or

(*l).propriedade;

I believe there’s no difference in performance and code, just different ways of doing the same thing, there’s some recommendation?

1 answer

3


The first is the most suitable for sure. In fact in essence no one does the second except to demonstrate that it is possible (and that it is a syntax sugar) or if you have a very specific case (I’m speculating, I can’t think of a case where that’s necessary.)

The second option is definitely no longer similar to C#, on the contrary, it is much less similar to C#. It uses a pointer and C# has no pointer (in almost all cases, all of which almost all programmers use), so it makes no sense to make that comparison. You’re comparing a silly syntax detail to be an arrow or a dot (a part of the syntax, and the rest is completely alien in C#, except in code unsafe, memsmo in this case would still use the arrow), with an important semantic detail that is to use a pointer explicitly or not. Semantics matter, syntax does not.

C equals C++ at this point.

What exists only in C++ is the reference, this yes is more similar to what is C#, because it uses the point to reference something and not to access a pointer. So if an object is accessed by a reference it can use semantics and syntax identical to C# (without counting certain details that only make sense in one language or another), after all in C# everything is reference and non-pointer.

It would be something like that:

void Metodo(Tipo &objeto) {
    objeto.membro;
}

If I were a pointer I’d access it like this:

void Metodo(Tipo *objeto) {
    objeto->membro;
}

I put in the Github for future reference.

There is no difference in performance in any case reported above.

The question had more information, so some things might seem odd, I answered according to the original version.

  • Sorry but I don’t find 'detail silly' I mean that when you use a class in C# and use obj.property and not obj->property, so 'I found' this feature. The question is not whether it is a pointer or not (of course it is) is whether there is any recommendation for the language, I will edit it to clarify the question, I believe I did not know how to express myself right.

  • Yes, you can think what you want, but it’s still a silly detail, by any criteria you use, just say that a dot is prettier than a comma, it’s dumber than discussing whether to use space or tab, if keys are better than begin and end, each has its own preference, but changes nothing in what it is doing, is bike sheding. I answered your question (you’re on the first line), you just stuck to another silly detail. instead of sticking to what matters in the answer, I don’t have to change a comma in my answer because of the editing. I can even improve the text, but the content is this.

  • Okay, relax. No stress

  • I have to challenge you because you didn’t argue anything, if you read the sentence that says you don’t think it’s a silly detail, you’re just saying, you think it, you have no argument. So if you think it’s better to use the second way, you can use it, it’s horrible, it’s less readable, no one does it, it has nothing to do with C#, nothing, except that it has a point among other things, so it’s like saying that people look alike because the whites of the eyes are the same.

  • okay. was just to say, this is not the standard of the community c++, and no, this is a 'silly detail',' it’s horrible,' I think the tone of the answer is not much that the community values(ok you are moderator and know more than I do about it)I think you’re tired of answering dummy questions and you’re in a bad mood.. relax.

  • Again, it is said in the first line, you who are clinging to a silly detail of the answer. If you think it’s cute you can wear it, but anyone else will say it’s horrible to do it, which is why in almost 50 years ago someone created a sugar syntax to avoid the use of something horrible, otherwise they would not have created it. I’m giving an answer as objective as the question asks, it asks to talk about coding style, it involves some subjectivity, and I’m giving you several arguments because that choice was made (not by me, but by everyone).

  • The bad mood seems to be just yours, I’m in a very good mood, I find these choices very funny. If you start reading the texts without letting your bad mood interpret it will be better. You saw something bad in me talking in silly detail, I just saw something good, it’s relevant information, and I gave you arguments because it’s silly and you shouldn’t worry about it, especially the way you’re worrying. You think the question dummy, I don’t think, are the ones I like to answer because it shows a suitable path within my experience.

  • The reference syntax is incorrect, it should be void Metodo(Tipo &objeto) (note the & before the identifier).

  • And a reason to use (*a).b about a->b in C++ is when you want to not use the overload of operator-> of a (if there is a).

  • And did not understand the reason to bring C# in the discussion, the author of the question did not question the comparison between languages. It sounds like a red Herring.

  • 1

    Yes, I did, thank you. https://answall.com/revisions/402090/1

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.