C++11 auto declaration with and without pointer declarator

后端 未结 6 846
借酒劲吻你
借酒劲吻你 2021-02-03 17:08

What\'s the difference between the types of bar1 and bar2?

int foo = 10;
auto bar1 = &foo;
auto *bar2 = &foo;

6条回答
  •  孤独总比滥情好
    2021-02-03 17:53

    As others said, they'll generate the same code. The asterisk is line noise (and makes it harder to switch from raw pointers to smart pointers if, for example, &foo is ever replaced by get_foo()). If you want to be explicit, then by all means, be explicit; but when you're using type inference, just let the compiler do its job. Lack of asterisks does not imply that an object isn't a pointer.

提交回复
热议问题