(我知道)在c ++中,我可以 declare variable超出范围,除了初始化全局/静态变量之外,我不能运行任何代码/语句.
理念
使用下面棘手的代码以便(例如)做一些 std::map操作是一个好主意吗?
在这里,我使用void *fakeVar并初始化它Fake::initializer()并做任何我想要的东西!
std::map<std::string, int> myMap;
class Fake
{
public:
static void* initializer()
{
myMap["test"]=222;
// Do whatever with your global Variables
return NULL;
}
};
// myMap["Error"] = 111; => Error
// Fake::initializer(); => Error
void *fakeVar = Fake::initializer(); //=> OK
void main()
{
std::cout<<"Map size: " << myMap.size() << std::endl; // Show myMap has initialized correctly :)
}
Run Code Online (Sandbox Code Playgroud) 经过多年的c ++编码,今天我被问到一个简单的问题,但实际上我找不到答案,所以我在这里.
除了想知道为什么会发生这个错误之外,我想知道如何通过修改模板函数来解决下面的错误(不改变main()函数)
template <class T>
T Add(T first, T second)
{
return first + second;
}
int main()
{
auto sample_1 = Add(1, 2); // Works
auto sample_2 = Add(1.f, 2.f); // Works
auto sample_3 = Add(1.f, 2); // Error: no instance matches the argument types: (double, int)
return 0;
}
Run Code Online (Sandbox Code Playgroud) 你有什么想法用"fork()函数"和"共享内存"块来模拟线程 ......
可能吗 ?
为程序执行此操作有多合理?(我的意思是,它会运作良好吗?)
我需要编写一个内核模块来计算Linux内核定时器(中断)频率.
有人告诉我,我需要在我的模块中使用计时器,但我不知道如何清楚地做到这一点:(
我的最终目标是将结果(频率)写入某个文件中(例如:/ proc/osfreq /).
=)
这可能看起来有点愚蠢,但我很想知道当我ping回环IP地址时到底发生了什么ping 127.0.0.1.
OS是否以特殊方式对待它?
有人可以告诉我为什么C编译器在使用a Compound Assignment和a Prefix Dec/Inc时输出错误?[但C++不]
int myVar = 5;
(--myVar) -= 4;
// C : error C2106: '-=' : left operand must be l-value
// C++: myVar=0;
Run Code Online (Sandbox Code Playgroud)
我知道错误说的是什么......
但是,我无法理解为什么C编译器无法识别myVar为l值而是C++?
我有一个空的git存储库(托管在GitHub或任何地方)。
我想匿名提交对它的更改,如下图所示。我应该如何设置我的git配置(最好是项目特定的配置)来实现?

谢谢
PS:如果您要问,但是为什么呢?,请查看会议“ 双盲”审查流程。
我想知道为什么在c ++中不能使用父类构造函数来获取特定的签名,以防派生类错过了什么?
例如,在下面的示例中,我无法初始化dd对象std::string.
#include <iostream>
class Base
{
int num;
std::string s;
public:
Base(int _num){ num = _num;}
Base(std::string _s){ s = _s;}
};
class Derived : public Base {
public:
Derived(int _num):Base(_num){}
};
int main()
{
Base b(50);
Derived d(50);
Base bb("hell");
Derived dd("hell"); // <<== Error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
随着继承我希望扩展一个类,而不是失去以前的功能,但在这里我感觉失去一些.
在一个更实际的例子中,我创建了我的版本,std::string但它std::string在某些情况下表现不像:
#include <string>
#include <iostream>
class MyString: public std::string {
public:
void NewFeature(){/* new feature implementation*/}
};
int …Run Code Online (Sandbox Code Playgroud) 我想了解如何在C++中编译类.
为什么下面的代码编译成功?是否Foo()需要编译实现才能成功?
class Test{
public:
Test() {}
int Foo();
};
int main()
{
Test obj;
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×6
c ×2
c++11 ×1
class ×1
compilation ×1
constructor ×1
fork ×1
git ×1
git-commit ×1
inheritance ×1
initializer ×1
linux ×1
linux-kernel ×1
module ×1
oop ×1
ping ×1
static ×1