4 ms·
It's possible, no matter where the object lives: #include <cstdio> using namespace std; class Type { public: Type(int a):_a(a){};
by ajuc 13y ago
It's possible, no matter where the object lives:
#include <cstdio>
using namespace std;
class Type {
public:
Type(int a):_a(a){};
int _a;
};
Type& getType(int a) {
Type* t = NULL;
t = new Type(a);
return (*t);
}
Type& doSthWith(Type& t) {
//no need (and no way) to check if t is reference to proper object
t._a=t._a+1;
return t;
}
int main() {
Type& t = doSthWith(getType(5));
printf("%d\n",t._a);
return 0;
};
This can be broken of course, for example by
Type& getType(int a) {
Type* t = NULL;
return (*t);
}
But that's another matter.
- chongli 13y agoRight. What makes Haskell special is not that it has option types, it's the fact that most types in Haskell cannot be null. This allows us to write pure (and total) functions, knowing that the compiler will enforce it; giving us a high degree of assurance that our function will not fail.