我读高级Linux编程第2章:
http://www.advancedlinuxprogramming.com/alp-folder/alp-ch02-writing-good-gnu-linux-software.pdf
在本节中2.1.3 Using getopt_long
,有一个示例程序有点像这样:
int main (int argc, char* argv[]) {
int next_option;
// ...
do {
next_option = getopt_long (argc, argv, short_options, long_options, NULL);
switch (next_option) {
case ‘h’: /* -h or --help */
// ...
}
// ...
Run Code Online (Sandbox Code Playgroud)
引起我注意的是next_option被声明为int.函数getopt_long()显然返回表示其在下面的switch语句中使用的短命令行参数的int.为什么可以将该整数与switch语句中的字符进行比较?
是否有从char(单个字符?)到int的隐式转换?上面的代码如何有效?(参见链接pdf中的完整代码)
有没有办法自动为所有派生类执行此操作,我不必为所有嵌套类创建函数applyPack.
这是我的代码:
/** every class has registered id with this function */
template<typename T>
uint getID() {
static uint id = registerClass<T>();
return id;
}
class TemplatesPack {
public:
template<typename T>
typename T::Template get();
};
class Object {
public:
virtual void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object>()); };
};
class Object2: public Object {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object2>()); };
};
class Object3: public Object {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object3>()); };
};
class Object4: public Object2 {
public:
void …
Run Code Online (Sandbox Code Playgroud) 有人可以解释以下代码的输出吗?
#include <iostream>
template <class T>
void assign(T& t1, T& t2){
std::cout << "First method"<< std::endl;
t1 = t2;
}
template <class T>
void assign(T& t1, const T& t2) {
std::cout << "Second method"<< std::endl;
t1 = t2;
}
class A
{
public:
A(int a) : _a(a) {};
private:
friend A operator+(const A& l, const A& r);
int _a;
};
A operator+(const A& l, const A& r)
{
return A(l._a + r._a);
}
int main ()
{
A a = …
Run Code Online (Sandbox Code Playgroud) 我写了一个简单的测试来检查c ++ 0x有多好.这是示例C++代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#ifndef __GXX_EXPERIMENTAL_CXX0X__
#define emplace_back push_back
#define auto typeof(vs.begin())
#endif
int main()
{
vector<string> vs;
string s;
while(cin>>s)
{
vs.emplace_back(s);
}
sort(vs.begin(),vs.end());
for(auto it = vs.begin();it != vs.end();++it)
{
cout << (*it) << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一个运行它的脚本
#!/bin/bash
inputFile=`mktemp`;
outputFile1=`mktemp`
outputFile2=`mktemp`
cat /dev/urandom | base64 > $inputFile 2> /dev/null &
echo "Generating Sample Input.. ${1:-10} seconds"
sleep ${1:-10}
export TOKILL=`pgrep -P $$ cat`
$(kill $TOKILL) …
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
int main() {
int *i = nullptr;
delete i;
}
Run Code Online (Sandbox Code Playgroud)
问题: