0

Is there any significant difference between the two statements below?

MyClass &ref = *(new MyClass);

MyClass *ptr = (new MyClass);
7
  • 1
    Pointer can be changed. Reference can't. Commented Sep 11, 2018 at 11:49
  • 2
    BTW: the outer parentheses are useless. Commented Sep 11, 2018 at 11:50
  • 2
    why would you write any of the two? Prefer MyClass obj; Commented Sep 11, 2018 at 11:50
  • 1
    While MyClass &ref = (*(new MyClass)); is technically correct C++ code, it goes against most conventions and leads to "WTF" code full of errors, so please, don't use that. Commented Sep 11, 2018 at 11:52
  • 2
    One significant difference is that you learn pretty quickly not to use the first. Learning to not use the second usually takes longer. Commented Sep 11, 2018 at 11:56

3 Answers 3

4

First is a reference, second is a pointer. Reference cannot be changed.

Overall avoid handrolled memory management (this means not writing new/delete at all)

Sign up to request clarification or add additional context in comments.

Comments

2

While pointers can be changed, and references cannot, I would still advise against manually manipulating pointers.

A destructor/constructor based memory management can solve so much headache down the line.

I would also consider using smart pointers.

What is a smart pointer and when should I use one?

Comments

0

Speaking technically, not hugely.

I mean, you can't rebind ref to some other thing later, in the same way that you can make ptr point to something else (though that's arguably a good thing).

The big issue is in expressing intent. We're all conditioned to see a pointer and think "aha, this may be dynamically allocated, and if nothing else I need to be aware of this object's lifetime and ownership semantics to see whether it must be deleted" (and, if you don't, it's leaked). This is practically never the case with a reference, which we generally take to mean either of:

  • The referent is not dynamically allocated, or
  • The scope that passed me this reference does not intend for me to do anything relating to the referent's lifetime

You're just supposed to use the value and leave it at that.

So, the biggest problem is in clarity of code via conventional coding styles.

But if you were some sort of practical joker you could write the following and it would be perfectly "valid":

int main()
{
   int& ref = *(new int);
   delete &ref;
}

Just remember to use the address-of operator, to get a pointer again for delete!

  Expression    Type    Indirection
                            ∧
    &&expr       T****      |
     &expr       T***       |
      expr       T**        |
     *expr       T*         |
    **expr       T          |
                            ∨

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.