我有两个课程,实体和水平.两者都需要访问彼此的方法.因此,使用#include会产生循环依赖的问题.因此,为了避免这种情况,我尝试在Entity.h中转发声明Level:
class Level { };
Run Code Online (Sandbox Code Playgroud)
但是,由于Entity需要访问Level中的方法,因此它无法访问此类方法,因为它不知道它们存在.有没有办法解决这个问题,而无需重新声明实体中的大部分级别?
I know I cannot use a namespace as a template parameter. However, I'm trying to achieve behavior similar to this:
template <typename T>
void foo(T::X* x)
{
T::bar(x);
}
Run Code Online (Sandbox Code Playgroud)
Except T is a namespace rather than a struct or a class. What is the best way to achieve the most similar result to what I am expecting?
对于我的生活,我无法使用此代码.我试图在XNA框架的弃用之后将我的代码从C#转换为C++,但是一个顽固的方法不希望被转换.在C#中它是:
public Tile GetTileAtPosition(bool screenOrGame, Vector2 position)
{
if (screenOrGame)
{
return Array.Find(tileList, tile => tile.Position == position / 24);
}
else
{
return Array.Find(tileList, tile => tile.Position == position);
}
}
Run Code Online (Sandbox Code Playgroud)
在C++中,我试图用来代替这个的代码是:
Tile Level::GetTileAtPosition(bool screenOrGame, sf::Vector2f position)
{
vector<Tile>::iterator it;
if (screenOrGame)
{
it = find(tileList.begin(), tileList.end(), [position](const Tile &t) { return t.GetPosition() == sf::Vector2f(position.x / 24, position.y / 24); });
return Tile(it->GetID(), it->GetPosition().x, it->GetPosition().y);
}
else
{
it = find(tileList.begin(), tileList.end(), [position](const Tile& t) { return t.GetPosition() == …Run Code Online (Sandbox Code Playgroud) 假设您有一个经常重复的常见工作流程,但有一些变化:
我正在尝试实现一种机制,可以自动执行任意操作(在C++ 98中).例如,以下内容:
myMutex.acquire();
int a = foo(arg1, arg2, arg3);
myMutex.release();
return a;
Run Code Online (Sandbox Code Playgroud)
可能成为:
return doMutexProtected(myMutex, foo, arg1, arg2, arg3);
Run Code Online (Sandbox Code Playgroud)
或者一些类似的机制.挑战在于如何为任意类型a和任意类型和数量的参数执行此操作.
我有一种感觉,应该有一种方法来使用模板,但我不知道如何实现它.你可以用仿函数做类似的事情,但你必须提前告诉仿函数他们的参数类型 - 我希望有一种方法可以从被调用的原始函数中自动检测它们.这样,如果(当)函数的参数列表发生变化时,除了要调用的参数列表之外,您不必更新任何内容.
这可能吗?
c++ ×4
c++98 ×2
templates ×2
c++03 ×1
c++11 ×1
declaration ×1
dependencies ×1
expression ×1
find ×1
forward ×1
lambda ×1