0

To my knowledge, foo->bar() is the same as (*foo).bar(), except when the arrow is overloaded in a weird way, which usually should not be done. This answer seems to concur.

This however seems to not be true for boost::shared_ptrs. Above are two lines of my code that yield different results in some cases:

(*new_link).getRefNode()  // returns some value
new_link->getRefNode()    // returns something different...

What am I missing / not understanding here?

Edit: As requested, the cout code and what gets printed. I made sure that getRefNode() does not modify the instance in any way - if I change the order of the calls, the values change with the calls, so it's not about anything that getRefNode() does internally.

    std::cout << "BLink1: " << (*previous_link).getId().toStdString()
              << ", ref: " << (*previous_link).getRefNode()
              << ", nref: " << (*previous_link).getNrefNode()
              << std::endl << "Link2: " << (*new_link).getId().toStdString()
              << ", ref: " << (*new_link).getRefNode()
              << ", nref: " << (*new_link).getNrefNode() << std::endl;
    std::cout << "New link: " << new_link->getId().toStdString()
              << ", ref: " << new_link->getRefNode()
              << ", nref: " << new_link->getNrefNode() << std::endl;

BLink1: 1076889319, ref: (48.68674, 8.99122), nref: (48.68682, 8.99368)

Link2: 1076570435, ref: (48.68674, 8.99122), nref: (48.68682, 8.99368)

New link: 1076570435, ref: (48.68669, 8.98889), nref: (48.68674, 8.99122)

(Highlights by me)

Thomas
  • 4,696
  • 5
  • 36
  • 71
  • 4
    Please show a [mcve]. – n. m. could be an AI Jul 19 '17 at 08:45
  • How have you tested this return value? – Stefan Jul 19 '17 at 08:52
  • My problem is that I have no idea what causes this, so I cannot write a minimal verifiable example... – Thomas Jul 19 '17 at 08:53
  • @Stefan: Not sure what you mean. I do a cout on the return value and it yields, in some out of many cases, different results. Most of the time it works as expected. – Thomas Jul 19 '17 at 08:54
  • Can you post the code with the `cout`? It might add some relevance. – Stefan Jul 19 '17 at 08:55
  • I appended it to my question. – Thomas Jul 19 '17 at 08:59
  • 3
    Tell me: If you reverse the order of the calls (i.e. call `->` first) do the values in the third line move to the second? – StoryTeller - Unslander Monica Jul 19 '17 at 09:03
  • Is that possible that you have modified some value by accident between two `cout`? Are 2 `cout` consecutive? – CWLiu Jul 19 '17 at 09:06
  • @StoryTeller Yes, they do. I tried calling the "Link2" line again afterwards to be sure and it still delivers the wrong results, so it is not about the order... – Thomas Jul 19 '17 at 09:06
  • @CWliu: No, nothing happens between the couts, I execute this part of the Code as posted... – Thomas Jul 19 '17 at 09:10
  • 2
    Well, `boost::shared_ptr` returns a `T*` when `operator->` is applied to it. Such a perturbation seems to me like an indicator of UB somewhere in your class. – StoryTeller - Unslander Monica Jul 19 '17 at 09:11
  • 2
    `operator*` returns a reference and `operator->` returns a pointer which ought to point at the same thing. If you suspect that it in fact points at something else, print it together with the address of the result of `operator*` and verify. If they are the same, then `operator*` and ``operator->` work correctly and the bug is elsewhere. – n. m. could be an AI Jul 19 '17 at 09:28
  • 1
    If you don't chain the `<<`s, do you get the same result? – molbdnilo Jul 19 '17 at 09:29
  • @n.m.: How would you print what operator-> returns? I tried it with, but operator-> returns an r-value, it has no address... – Thomas Jul 19 '17 at 10:20
  • @molbdnilo: Thanks for that suggestion. They print the correct thing if they are not chained... Which is still confusing for me. I'll edit the question. – Thomas Jul 19 '17 at 10:39
  • 1
    @Thomas Then one or more of the function must have a side effect that affects the result of some of the others. The order of evaluation of operands (like all function arguments) is unspecified, so you need to be careful about that. – molbdnilo Jul 19 '17 at 10:46
  • Hm, so what is the correct procedure on SO for this? Completely edit my question, or close this one and open a new one? I still don't know why this happens, but it's not about * and -> any more... – Thomas Jul 19 '17 at 10:47
  • 2
    What operator-> returns *is* an address. You print *it*. `cout << smartptr.operator->();`. – n. m. could be an AI Jul 19 '17 at 10:50
  • @n.m. Oh, I just read about that. I tried it and they do indeed return the same address, which means the problem is in the chaining and not in -> and *. I'll close this question and ask a new one since I would have to edit too much here. Thanks! – Thomas Jul 19 '17 at 10:58

1 Answers1

0

It turns out that the problem is not in the difference between (*a) and a->, but in chaining them together in the same cout. Doing cout << a->func() << b->func(); yields different results from cout << a->func(); cout <<b->func();.

I'll open a new question for that.

Thomas
  • 4,696
  • 5
  • 36
  • 71