假设我有以下课程:
class Parent
{
private int ID;
private static int curID = 0;
Parent()
{
ID = curID;
curID++;
}
}
Run Code Online (Sandbox Code Playgroud)
和这两个子类:
class Sub1 extends Parent
{
//...
}
Run Code Online (Sandbox Code Playgroud)
和
class Sub2 extends Parent
{
//...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是这两个子类是从父类共享相同的静态curID成员,而不是具有不同的子类.
所以,如果我这样做:
{
Sub1 r1 = new Sub1(), r2 = new Sub1(), r3 = new Sub1();
Sub2 t1 = new Sub2(), t2 = new Sub2(), t3 = new Sub2();
}
Run Code Online (Sandbox Code Playgroud)
r1,r2,r3的ID为0,1,2,t1,t2,t3的ID为3,4,5.而不是这些我想要t1,t2,t3具有值0,1,2,即使用curID静态变量的另一个副本.
这可能吗?如何?
当在cygwin中遇到dll /分叉错误时,我正在调用rebaseall脚本,一切都神奇地再次起作用.我知道它以某种方式修改了cygwin安装中的dll,因为我在有问题的和重新安装的那些之间运行了差异.
它在这些二进制文件中完全修改了什么并使它们再次起作用?
我正在寻找一种方法来抑制我可能通过pragma指令获得的所有可能的警告.我制作了一些警卫宏来帮助我让第三方标题从警告中消失,而现在它们对于msvc和clang来说就像魅力一样.我仍然缺少使用Gcc诊断编译指示的正确方法来抑制部分中的每个警告.我举几个例子:
在msvc中我们可以这样做:
#pragma warning(push, 0)
// Code that produces warnings...
#pragma warning(pop)
Run Code Online (Sandbox Code Playgroud)
在clang我们可以做到这一点:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wall"
#pragma clang diagnostic ignored "-Wextra"
// Code that produces warnings...
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)
中间的代码现在正在被警告保持沉默.
在Gcc中我们也有类似的pragma指令与clang,我想我可以尝试这样的事情:
#pragma GCC diagnostic push
#pramga GCC diagnostic ignored "-Wall"
#pragma GCC diagnostic ignored "-Wextra"
// Code that produces warnings...
#pramga GCC diagnostic pop
Run Code Online (Sandbox Code Playgroud)
但是在GCC中传递-Wall和-Wextra在诊断忽略的pragma中并不像clang那样工作,并且不会禁用所有可能的警告.而不是传递一个特定的警告来禁用工作:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void foo (int x) // No longer getting "unused …Run Code Online (Sandbox Code Playgroud) 我正在使用最新的快速构建clang与最新的TDM-Gcc头文件和库.编译时(使用-std = c ++ 11标志):
#include <functional>
#include <iostream>
class Foo
{
public:
void Bar(int x)
{
std::cout << x << std::endl;
}
};
int main()
{
Foo foo;
auto f = std::bind(&Foo::Bar, &foo, 5);
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到这些错误:
In file included from Test.cpp:1:
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1426:7: error: static_assert failed "Wrong number of arguments for pointer-to-member"
static_assert(_Varargs::value
^ ~~~~~~~~~~~~~~~
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1440:7: note: in instantiation of template class 'std::_Bind_check_arity<void (Foo::*)(int) __attribute__((thiscall)), Foo *, int>' requested here
: _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
^
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1461:5: note: …Run Code Online (Sandbox Code Playgroud) 使用 GCC 编译 C/C++ 源代码时,可以使用 flags 以 Makefile 规则形式创建附加依赖信息-MMD -MT $@ -MF $(basename $@).d。我使 Makefile 脚本与 GCC 和 MSVC 工具链兼容,但我仍然在 MSVC 上生成依赖文件方面遇到困难。
有一个标志/showIncludes
以以下形式在 stdout 中输出源文件包含信息Note: including file: filename。尝试解析它(到目前为止成功)我得到了以下 makefile 函数:
msvc-dep-gen = echo $@: $< |\
sed -e "s/^.*$$/&\\/" >$(basename $@).d && \
$(1) /showIncludes |\
sed -e "/^Note: including file:/!d"\
-e "s/^Note: including file:\s*\(.*\)$$/\1/"\
-e "s/\\/\//g"\
-e "s/ /\\ /g"\
-e "s/^\(.*\)$$/\t\1 \\/" >> $(basename $@).d
Run Code Online (Sandbox Code Playgroud)
其中参数 $(1) 是使用 MSVC 编译给定源文件的包装命令。这会很好地生成依赖文件,但输出会被过滤掉,因此我会丢失编译器产生的所有警告和错误退出。关于如何防止这种情况有什么聪明的想法吗?
我需要帮助理解松耦合.当子对象需要与父对象通信时,如何设计一个使用组合松散耦合的类?让我举个例子:
我们有这个:
class A {
private:
B b;
public:
void foo();
};
Run Code Online (Sandbox Code Playgroud)
B对象如何从容器类A调用函数foo()?显而易见的答案是"只是将指针从A传递到b",但这是紧密耦合,而且设计不灵活.
你能给我一个简单的解决方案来解决这个问题(最好用C++或Java)或提供处理这些问题的设计技巧吗?
我的真实案例来自为JRPG开发游戏引擎.我有这门课:
class StateMachine
{
private:
std::map<std::string, State*> states;
State* curState;
public:
StateMachine();
~StateMachine();
void Update();
void Render();
void ChangeCurState(const std::string& stateName);
void AddState(const std::string& stateName, State* state);
};
Run Code Online (Sandbox Code Playgroud)
在每个调用的游戏循环Update()中StateMachine,调用Update()函数curState.我想要curState能够ChangeCurState从StateMachine班级调用,但松耦合.
我有一组公钥/私钥,在仅使用加密和解密的两种方法之一加密/解密某些数据时,它可以完美无缺。
我仍然没有运气尝试用两者之一加密数据并用另一个解密。
示例方案:
a) 我使用公钥和以下 node.js 代码创建了一些加密数据:
#!/usr/bin/env node
var NodeRSA = require('node-rsa');
var fs = require('fs');
function createUsingPubKey(Pub, data) {
var pk = new NodeRSA();
pk.importKey(Pub);
encrypted = pk.encrypt(data, 'base64');
return encrypted;
}
var sampledata = "SECRET STUFF";
var genkey = createUsingPubKey(fs.readFileSync('id_rsa.pub'), sampledata)
console.log(genkey);
Run Code Online (Sandbox Code Playgroud)
b)然后我尝试使用 openssl 实用程序解密它:
node test.js | openssl base64 -d -A | openssl rsautl -inkey id_rsa
Run Code Online (Sandbox Code Playgroud)
但我得到:
RSA operation error
1068:error:0406706C:rsa routines:RSA_EAY_PUBLIC_DECRYPT:data greater than mod len:.\crypto\rsa\rsa_eay.c:680:
Run Code Online (Sandbox Code Playgroud)
我认为他们可能会在加密/解密过程中使用不同的算法,所以我前往这里的 node-rsa 文档:https : //www.npmjs.com/package/node-rsa,我找到了这个选项:
encryptionScheme — 加密/解密的填充方案。可以是“pkcs1_oaep”或“pkcs1”。默认'pkcs1_oaep'。 …
给定一个void some_func()在静态库中具有签名的编译函数,以及另一个void some_func()在目标文件中具有相同签名的函数,人们期望当它们将它们链接在一起时,应该发生"多重定义"错误.
但这种情况并非如此.据我所见,链接器(使用GCC和MSVC工具链测试)选择驻留在目标文件中的实现,而不会发出任何错误或警告.
鉴于以下POC:
somelib.h
#ifndef _SOMELIB_H_
#define _SOMELIB_H_
void some_func();
#endif /* _SOMELIB_H_ */
Run Code Online (Sandbox Code Playgroud)
somelib.c
#include "somelib.h"
#include <stdio.h>
void some_func()
{
printf("some_func in library\n");
}
Run Code Online (Sandbox Code Playgroud)
troublingheader.h
#ifndef _TROUBLING_HEADER_H_
#define _TROUBLING_HEADER_H_
void some_func();
#endif /* _TROUBLING_HEADER_H_ */
Run Code Online (Sandbox Code Playgroud)
troublingsource.c
#include "troublingheader.h"
#include <stdio.h>
void some_func()
{
printf("Troubling func\n");
}
Run Code Online (Sandbox Code Playgroud)
main.c中
#include <stdio.h>
#include "somelib.h"
int main()
{
some_func();
}
Run Code Online (Sandbox Code Playgroud)
还有一个简单的Makefile来帮助构建(首先创建tmp文件夹):
tmp/wat.exe: tmp/libsomelib.a tmp/main.c.o tmp/troublingsource.c.o
gcc -static -static-libgcc -Ltmp -otmp/wat.exe tmp/main.c.o tmp/troublingsource.c.o -lsomelib
tmp/main.c.o:
gcc -Wall …Run Code Online (Sandbox Code Playgroud) 我将轴对齐的边界框表示为 2 个 3D 向量,一个包含左下后点(最小值),一个包含右上前点(最大值)。
在向任何方向旋转对象后,必须重新计算包围它的 aabb 以适合。以此图为例:
可以使用我拥有的两个起点和旋转的角度+方向来计算吗?如果是这样怎么办?最有效的方法是什么?
PS 我正在使用 glm 进行数学计算,因此任何使用 glm 执行此操作的现成方法都将非常有用!
我知道在java中我们可以这样做:
class A<T extends B>
{
...
}
Run Code Online (Sandbox Code Playgroud)
我们可以用C++中的模板做同样的事情吗?例如,我想要一个模板化的类A,我们传递的模板T是另一个类B的子类,如上面的java示例.有任何想法吗?
编辑:在其中一个答案中,我被要求显示我的代码,因为我使用答案有链接错误.这是(从深到主):
//ComponentManager.h
...
template <typename T, typename std::enable_if<std::is_base_of<Component, T>::value>::type* = nullptr>
Component* AddComponent(rUUID uuid);
...
//ComponentManager.cpp
...
template <typename T, typename std::enable_if<std::is_base_of<Component, T>::value>::type* = nullptr>
Component* ComponentManager::AddComponent(rUUID uuid)
{
...
}
//Engine.h
...
template <typename T, typename std::enable_if<std::is_base_of<Component, T>::value>::type* = nullptr>
void AddComponent(rUUID);
...
//Engine.cpp
...
template <typename T, typename std::enable_if<std::is_base_of<Component, T>::value>::type* = nullptr>
void Engine::AddComponent(rUUID uuid)
{
...
}
...
//main.cpp
...
e.AddComponent<Position>(a->GetUUID());
...
Run Code Online (Sandbox Code Playgroud)