许多程序对许多参数和字符串数组使用标准名称.主要功能的原型如下:int main(int argc, char *argv[]);.但如果我为这些变量选择自定义名称,我会破坏一些东西吗?
例如 int main(int n_of_args, char *args[]);
在编译器的上下文中,一切都很好.这些变量对于main函数是本地的,因此它们可以具有任何名称.简单的代码构建和运行完美.但这些名称可能由预处理器使用.那么重命名这些论点是否安全?
PS我个人觉得这些名字很糟糕,因为它们看起来很相似,只有一个字母不同.但是每个人都出于某种原因使用它们.
在 C 中 double/float 有一个集合类型说明符:%f %F %g %G %e %E. 有什么区别吗
%f并且%F,%g并且%G,%e和%E?根据printf和scanf输出是相等的。那为什么大写和小写都有效呢?
请注意, scanf double 的类型说明符以小写开头 l
我们假设我有一段这样的代码:
#include <iostream>
int main()
{
int a = 5;
{
int a = 12;
std::cout << a;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想a==5从外部范围进行cout ,但是main::a肯定无法正常工作.有没有解决方法?
让我们想象一下情况:
#include <iostream>
int d =34;
namespace
{
int d =45;
}
int main()
{
std::cout << ::d ;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里的输出是34,因为::意味着全局命名空间.但如果我评论第3行输出是45,这很奇怪.
如果我使用std::cout << d ;- 我得到错误
s.cxx:12:15:错误:对'd'的引用是不明确的
在这种情况下,如何访问unnamed_namespace :: d?
PS:我已经读过,未命名的命名空间用于静态全局变量,仅在文件范围内可见
在C++中,可以在类中初始化类的字段的值,如:
class X
{
int a = 5;
}
Run Code Online (Sandbox Code Playgroud)
它是什么原因?它有用吗?默认的ctor完全相同.似乎我无法使用位掩码(int a : 3)初始化值.
Posix 支持阻塞和非阻塞文件描述符。第二个可以用标志打开O_NONBLOCK。我的应用程序中有一个主循环,它轮询一些poll文件描述符集(系统调用)POLLIN和POLLOUT事件。我是否仍可以使用阻塞文件描述符,因为我仅在POLLOUT设置时写入并仅在POLLIN设置时读取?
我可以轻松地声明匿名函数(它们与lambda相同,但没有"context" - [...])auto:
#include <iostream>
using namespace ::std;
void foo(void (*f)(char))
{
f('r');
}
int main()
{
void (*anon)(char) = [](char c) -> void { cout << c << endl; };
foo(anon);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是如何宣布lambda?这是唯一可能的方式吗?(也许使用typedef).我在这里使用::std::function,但我没有在foo参数中提到f的上下文...:
#include <iostream>
#include <functional>
using namespace ::std;
//int foo(auto f, char x) // !only since c++14
int foo(function<int(char)> f, char x)
{
return f(x+1);
}
int main()
{
int a = 5, b = 10;
//void (*lambda)(char) …Run Code Online (Sandbox Code Playgroud) 我重载operator*了std::string类,但在这种情况下:
std::string operator*(std::string a, unsigned b) //bad
{
unsigned old_length = a.length();
a.resize(a.length()*b);
for(unsigned i = old_length ;i<a.length()*b; i++)
a[i]=a[i%old_length];
return a;
}
Run Code Online (Sandbox Code Playgroud)
程序崩溃并出现错误:
***"./ program"中的错误:free():下一个大小无效(快):0x0000000000cd20b0***已中止
如果我像这样重载它 - 没有错误:
std::string operator*(std::string a, unsigned b)
{
unsigned old_length = a.length();
std::string a2 = a;
a2.resize(a.length()*b);
for(unsigned i = 0 ;i<a.length()*b; i++)
a2[i]=a[i%old_length];
return a2;
}
Run Code Online (Sandbox Code Playgroud)
那问题出在哪里?有没有办法不创建新的字符串a2?它消耗额外的内存.
#include <iostream>
#include <string>
std::string operator*(unsigned b, std::string a)
{
return operator*(a, b);
}
int main(int argc, char **argv) …Run Code Online (Sandbox Code Playgroud) 应该返回参考的最受欢迎的运算符是 operator=
class Alpha
{
int x;
int y;
std::string z;
public:
void print()
{ cout << "x " << x << " y " << y << " " << z << '\n'; }
Alpha(int xx=0, int yy=0, std::string zz=""): x(xx), y(yy), z(zz) {}
Alpha operator=(Alpha& another)
{
x = another.x;
y = another.y;
z = another.z;
return *this;
}
};
int main()
{
Alpha a(3,4,"abc"),b,c;
b=c=a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang说:
clang ++ - 3.6 new.cxx -o new new.cxx:70:3:错误:没有可行的重载'='b …