您可能知道 std::string
等数据类型具有一些功能。
例如 std::string.substr()
。
我想知道,有没有办法可以为这些类型添加更多功能?
例如:
int a = 42;
a.isLife(); // a function that returns true if a is 42
但是 isLife()
不存在吧?
有什么办法可以添加吗?
最佳答案
不,这在 C++ 中是不可能的。在 C++ 中,您改为编写 isLife(a)
。
关于c++ - 如何向内置类/类型添加更多函数,如 int、double、std::string 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57007414/