printf如果您可以访问#include <stdio.h>。printf由于格式说明符,我喜欢使用它,而且感觉比做的更好std::cout << "something\n";。
#include <stdio.h>
#include <cstdlib> // to use rand()
int main() {
int randNum = rand()%10 + 1;
printf("Hey, we got %d!\n", randNum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
与
#include <iostream>
#include <cstdlib> // to use rand()
using std::cout;
int main() {
int randNum = rand()%10 + 1;
cout << "Hey, we got " << randNum << "!\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我所在的不和谐服务器上的用户说这是不正确的做法,因为“ printf不安全,请使用std :: cout”。
第二个用户说这很不好,因为它不是类型安全的(第一个用户确实提到了类型安全,但没有提到深度)。
第二位用户说,
类型安全性由编译器强制执行。但是,通过使用可变参数函数,编译器无法在运行时确定参数的类型;它无法提前知道他们。该函数需要某种方式来说明期望的参数类型,而
printfC函数家族则通过格式字符串说明符完成了此操作。
所以我正在寻找其他选择。
如果没有,我想我会坚持 …
我正在研究 C++ 类。
我基本上是在重新制作我在这里所做的事情,但是在 C++ 中。
它进展顺利,但我不明白错误member reference type 'Human *' is a pointer; did you mean to use '->'?意味着什么。我从未使用过->,也见过*以这种方式使用过(例如const char *),但我不太确定它是如何工作的。
我发现的最接近的问题是这个,但答复没有帮助。
这是我的代码
#include <stdio.h>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::cin;
using std::string;
class Human {
public:
string Name;
int Age;
double Height;
void Initialise(string name, int age, double height) {
this.Name = name; // Error here
this.Age = age; // Error here
this.Height = …Run Code Online (Sandbox Code Playgroud) 因此,在Lua中,您可以执行以下操作
local function start(n)
return function()
n = n + 1;
return n;
end;
end
print(start(1)()); --> 2
Run Code Online (Sandbox Code Playgroud)
我听说过模板,但是我不想遇到xy问题,或者还有其他方法