我刚刚了解到编译器巧妙地将函数和变量的调用替换为它们代表的代码.考虑到这一点,第二种方法实际上会在下面更好(由于清晰度),实际上运行速度与第一种方法一样快?
//check to see if sphere intersects the box
bool BoundingBox::Intersects(BoundingSphere boundingSphere)
{
// check for intersection on each axis by seeing if the radius is large enough to reach the edge of the cube on the
// appropriate side. All must evaluate to true for there to be an intersection.
return (
((boundingSphere.Centre().x < negCorner.x && boundingSphere.Radius() > posCorner.x - boundingSphere.Centre().x) ||
(boundingSphere.Centre().x > posCorner.x && boundingSphere.Radius() > boundingSphere.Centre().x - negCorner.x))
&&
((boundingSphere.Centre().y < negCorner.y && boundingSphere.Radius() > posCorner.y …Run Code Online (Sandbox Code Playgroud) 我可以编写一个具有虚方法的类 - (可以被覆盖但具有默认行为)以及抽象方法 - (必须被覆盖)
我可以定义一个定义虚拟实现而不是摘要的实现吗?
另外,我可以创建在非抽象/纯虚拟类中没有实现的抽象方法吗?
当你必须将它们转换为整数时,枚举被用于像switch语句这样的东西有什么意义?
使用一些常量int不仅仅是更好吗?我想使用枚举,但演员使他们看起来很笨重.我以为它们本来就是彗星,但它们还是别的什么?
他们在C++中有相同的行为吗?
抱歉这个措辞严厉的问题,我的大脑很难受.
为了避免使用包含圈,我将以前在类头中的#include替换为其cpp文件.然后我在标题中放置了一个前向声明.
这解决了我的问题,但是,源文件似乎不再能够访问cpp文件中包含文件的成员.为什么是这样?什么是解决方案?
编辑 - 下面的真实代码:
标题:
#ifndef BULLET_H
#define BULLET_H
#include "BoundingSphere.h"
#include "helperMethods.h"
class GameObject;
class Bullet : public GameObject
{
private:
float velocity;
float distance;
D3DXVECTOR3 firedFrom;
BoundingSphere bSphere;
public:
Bullet(D3DXVECTOR3 position, D3DXVECTOR3 rotation, float velocity, D3DXCOLOR colour);
BoundingSphere BulletSphere();//change this to simply use position
void Update();
};
#endif
Run Code Online (Sandbox Code Playgroud)
资源:
#include "Bullet.h"
#include "GameObject.h"
Bullet::Bullet(D3DXVECTOR3 position, D3DXVECTOR3 rotation, float velocity, D3DXCOLOR colour)
: bSphere(BoundingSphere(position, 0.01f))
{
//Model = content.Load<Model>("Models\\basicMesh");
//Texture = content.Load<Texture2D>("Textures\\basicMesh");
distance = 0.0f;
Scale(D3DXVECTOR3(0.25f, 0.1f, 0.1f));// …Run Code Online (Sandbox Code Playgroud) 我在vector课堂上发生了一个错误,就像你在上课时所访问的课程一样#include < vector >
我只得到一次,我不知道它为什么会发生:
错误C2036
'Agent *const '::未知大小
这也发生在vector中,有错误的代码在这里:
size_type size() const
{ // return length of sequence
return (this->_Mylast - this->_Myfirst); // error on this line
}
Run Code Online (Sandbox Code Playgroud) 实例:
weapons.push_back(new Pistol());
weapons.push_back(new Rifle());
weapons.push_back(new Shotgun());
Run Code Online (Sandbox Code Playgroud)
析构函数,当第一次删除时,代码中断.当我关闭程序时会发生这种情况.
Brain::~Brain()
{
for (unsigned int i = 0; i < weapons.size(); i++)
{
delete weapons[i]; // this is where the code breaks
}
}
Run Code Online (Sandbox Code Playgroud)
我收到警告:
Unhandled exception at 0x0096371f in D3D10DEMO.exe: 0xC0000005: Access violation reading location 0x000002ce.
Run Code Online (Sandbox Code Playgroud)
武器是这样的:
weapons(vector<Gun*>())
Run Code Online (Sandbox Code Playgroud)
编辑 - 我已经删除了这个问题的大部分代码,但是我也减少了我的程序,以便在一个更小的解决方案中重现问题:
在Accelerated C++一书中,声明如果你声明这样的向量向量:
vector<vector<int>> foo;
Run Code Online (Sandbox Code Playgroud)
它可能会产生错误,因为>>可能被误读为运算符,所以你实际上应该像这样写它:
vector<vector<int> > foo;
Run Code Online (Sandbox Code Playgroud)
但是,我使用这些结构编写第一种方式的代码似乎有效.这是一个我应该遵循的现代约定吗?
这是一个简单的吸气剂吗?还是每次计算?
如果我有这样的for循环:
for (int i = 0; i < myVector.size(); i++) { }
Run Code Online (Sandbox Code Playgroud)
我只需要使用一次大小,在循环之前计算一次并将其存储在变量中会更好吗?或者size()只是一个简单的吸气剂,它会有什么不同?
我在一些代码中遇到了一个typedef,它是这样的:
typedef void (NE_API *NeWindowProcCallback)(void* hWnd, NEuint uMsgId, NEuint wParam, NEuint64 lParam);
Run Code Online (Sandbox Code Playgroud)
但是,我不熟悉这种语法.谁有人解释这个?
另外,如果我跳到NE_API的声明,我发现:
# define NE_API __stdcall
Run Code Online (Sandbox Code Playgroud)
我认为这可能与答案有关,对此的解释也将非常感激.谢谢.
我想在C#中重新初始化List - 基本上擦除它并再次填充它.
如果我通过使用new关键字分配给它再次初始化列表,那么旧的仍然在某处或者我是否已成功覆盖它?