'How to simplify the arrow(->) and scope resolution (::) operators in C++?

I'm a self-educated game designer moving to Unreal from Unity for some reasons.I think C# is perfect for me, but it may not be a good practice to use C# in Unreal with plugins or some other tricks, so I'm learning C++ now.

The most unbearable thing for me is the "->" and the "::" operators, which really affects my typing efficiency and fluency.

The expression such as ptr->member in C++ basically equals to object.member in C#, but the former makes it more difficult to read and write (for me), so is it there a way to type the former as conveniently as in C#?



Solution 1:[1]

You could use using and references to get to use syntax that's closer to C#. In fact I encourage you to use references, to pass around the "address" of objects, if you're sure the address cannot be null.

std::string foo;
std::string bar;
std::string* baz = &bar;
std::cout << baz->c_str() << '\n';

could be rewritten as

using std::cout;
using std::string;

string foo;
string bar;
// preferable alternative to the next 2 lined: auto& baz2 = bar;
auto* baz = &bar; // keeping this to demonstrate how to go from pointer to reference
auto& baz2 = *baz;
cout << baz2.c_str() << '\n';

Note that there is also using namespace to make everything from one namespace available in the current one, but in general I'd discourage this, since you easily loose control of the symbols this way.

using namespace std;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fabian