I’m just wondering whether this is possible or not in C++.
In PHP (frameworks) I sometimes see classes (objects) and methods within the class being accessed by:
$this->encrypt->decode($msg, $key);
In one of my programs I have the following:
Directory *d = new Directory("dir");
d->open();
Is it possible to have instead:
Directory *d = new Directory("dir")->open();
So the two methods can be executed in the same line?
The PHP code
$this->encrypt->decode($msg, $key);is calling thedecode()method onencryptwithin the current class.The code you have is performing a different task of initialising a class and then calling it.
In the same way you couldn’t do this in PHP:
.. you can’t do it in C++ either, no. You would not expect an instance of
Testin$testin this case, it would instead be the return ofmethod()on a newly instantiated test object.That said, you can indeed inline it like you say if you only needed the return value of
open()(or nothing at all) and not the instance ofDirectory: