-2

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#?

Reek
  • 1
  • 3
    `auto& ref = *ptr; ref.member;` – NathanOliver Apr 20 '22 at 17:52
  • 5
    Save pointers for when you really need them and mostly use references. That way you get the `object.member` syntax you are after. Scope resolution... Solves too many problems to go without. – user4581301 Apr 20 '22 at 17:53
  • 1
    Are you asking for how to set up your editor to make it easy to rope the arrow operator, or how to set up your code such that the arrow operator is unneeded? – Sneftel Apr 20 '22 at 17:53
  • 2
    Since `->` and `.` can be valid on the same object (e.g. shared_ptr), you will not really get around them. – Timbo Apr 20 '22 at 17:55
  • 4
    Side note: The real time expenditure is in debugging and maintenance, probably followed by design. Outside of coding competitions, how fast you write code pretty much doesn't matter because it is swallowed up by the rest of the job. It's better to write slowly and make sure the code's easy to read (and as a result debug and maintain) than it is to write quickly. – user4581301 Apr 20 '22 at 17:57
  • @Sneftel Maybe both. It's ok to just make it easy to rope them, but it's better to get rid of using them. – Reek Apr 20 '22 at 17:58
  • 2
    honest suggestion: get used to it. its C++, and if you are going to code in C++, you'd better get use to it. leave C# aside. dont fight it. let that sink in, accept it as an undeniable fact of reality and make peace with it. – Hossein Apr 20 '22 at 18:07
  • 1
    Side note: Self education in C++ is a good path, but do yourself a huge favour and invest in [some good books](https://stackoverflow.com/questions/388242/) if you haven't already. Pretty much everything you need is somewhere on the Internet, but A) it's damn hard to find if you don't know the right terminology and B) how do you know the web page you're reading wasn't written by an [idiot with a grossly inflated estimation of their abilities](https://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect)? – user4581301 Apr 20 '22 at 18:08

1 Answers1

2

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;
fabian
  • 80,457
  • 12
  • 86
  • 114