小编Sir*_*lot的帖子

构造函数完成运行时,指针变为null

我有一个相机类,有两个指针作为参数:

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX* matProjection, D3DMATRIX* matView)
{
    this->SetCamera(fOVDeg, nearCull, farCull);
    this->AdjustCamera(xPos, yPos, zPos);

    matProjection = &(this->projection);//gets the addresses of two private members
    matView = &(this->view);
}
Run Code Online (Sandbox Code Playgroud)

这是调用它的代码:

D3DMATRIX* matProjection,* matView;

//called once
void Initialise(HWND hWnd)
{
    camera = new Camera(
            45.0f, 1.0f, 100.0f,
            0.0f, 9.0f, 24.0f,
            matProjection, matView);

...rest of code...
Run Code Online (Sandbox Code Playgroud)

基本上问题是我想要代码中的两个指针调用相机构造函数来保留相机类的两个私有成员的内存地址,所以我可以用它们来做事!麻烦的是,这工作正常,但一旦构造函数完成,指针就变为空(0x00000000).我不知道为什么!相机的私人成员仍然有价值观,因为我之前只是用吸气剂抓住他们的价值而且它运作良好.

c++ constructor pointers

0
推荐指数
1
解决办法
1095
查看次数

变量与长算法

使用这两种算法有什么区别.我一直想知道我应该如何优化它们.他们如何区分内存和速度?这个比那个好吗?除了代码清晰度,我的意思是.

这是我的第一个版本:

bool Intersects(BoundingSphere boundingSphere)
{
    D3DXVECTOR3 vectorBetween = (centre - boundingSphere.centre);

    // works out the distance between the sphere centre's using pythag
    float distance = sqrt(
                         pow(vectorBetween.x, 2) 
                       + pow(vectorBetween.y, 2) 
                       + pow(vectorBetween.z, 2));

    // if two radius's add to more than the distance between the centres
    return (radius + boundingSphere.radius > distance);
}
Run Code Online (Sandbox Code Playgroud)

这个方法是相同的,但它不包含变量中的任何值,它只使用一个长计算

bool Intersects(BoundingSphere boundingSphere)
{
    return (radius + boundingSphere.radius >
            (sqrt(pow((centre - boundingSphere.centre).x, 2) +
                  pow((centre - boundingSphere.centre).y, 2) +
                  pow((centre - boundingSphere.centre).z, 2))));
}
Run Code Online (Sandbox Code Playgroud)

c++ performance

0
推荐指数
1
解决办法
123
查看次数

奇怪的慢方法

我在我的项目中有一个方法,当它放入自己的程序时只需几秒钟就可以运行,当它在它所属的项目中运行时需要5分钟.我不知道为什么.我尝试过分析,取出比特,改变这一点.我很难过.

它填充了另一个类使用的整数向量,但此类目前尚未实例化.我尽可能多地检查,看起来确实没有其他事情发生,但是这个方法在一个项目中神奇地花费的时间比在另一个项目中花费的时间长.

该方法在启动时运行,大约需要5分钟,如果单独运行则需要大约3秒钟.可能是什么导致了这个?奇怪的项目设置?多线程的东西,我不知道?(据我所知,无论如何我的项目都没有,除非它是自动完成的).

还有就是该项目的链接在这里.如果有人能为我解决这个问题,我将非常感激,并且我能尽快为此开始赏金.

该方法称为PopulatePathVectors,并在Level.cpp中运行.注释掉对方法的调用(在关卡构造函数中)意味着程序在几秒钟内启动.使用它生成的列表的唯一其他类是代理,但目前没有实例化.

编辑 - 根据要求,这是代码.虽然请记住我的问题不是'为什么代码慢?' 但是"为什么它在一个地方快速而且在我的项目中变慢?"

//parses the text path vector into the engine
void Level::PopulatePathVectors(string pathTable)
{
    // Read the file line by line.
    ifstream myFile(pathTable);

        for (unsigned int i = 0; i < nodes.size(); i++)
        {
            pathLookupVectors.push_back(vector<vector<int>>());

            for (unsigned int j = 0; j < nodes.size(); j++)
            {
                string line;

                if (getline(myFile, line)) //enter if a line is read successfully
                {
                    stringstream ss(line);
                    istream_iterator<int> begin(ss), end;
                    pathLookupVectors[i].push_back(vector<int>(begin, end));
                }
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

编辑 …

c++

0
推荐指数
1
解决办法
490
查看次数

sql教程脚本错误

它告诉您从教程复制并粘贴到phpMyAdmin的SQL页面的SQL文本给出了错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'TYPE=MyISAM' at line 6
Run Code Online (Sandbox Code Playgroud)

这是脚本:

CREATE TABLE `scores` (
   `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
   `name` VARCHAR(15) NOT NULL DEFAULT 'anonymous',
   `score` INT(10) UNSIGNED NOT NULL DEFAULT '0'
)
TYPE=MyISAM;
Run Code Online (Sandbox Code Playgroud)

我在这里弄错了什么?

php mysql sql phpmyadmin

0
推荐指数
1
解决办法
1406
查看次数

为什么我的类的构造函数在这种情况下没有被调用?

我正在创建一个像这样的新类实例:

Cube* cube1;
Run Code Online (Sandbox Code Playgroud)

Cube构造函数中有代码,但它没有运行!这通常吗?

c++

-1
推荐指数
1
解决办法
136
查看次数

没有构造函数的类强制使用指针类型?

我不想在没有必要时使用指针,但是这里有问题:在下面的代码中,如果我删除星号并level简单地做一个object,当然删除行level = new Level;我得到运行时错误,原因是其level然后被初始化在第一行,BEFORE initD3Dinit_pipeline-即设置用于在投影和视图中的方法.你看到的问题是level使用这两件事,但是当我第一次完成时,我得到一个null指针异常.

这只是一个答案是使用指针的情况吗?我之前遇到过这个问题,基本上看起来非常令人烦恼的是,当一个类类型不接受任何参数时,你基本上是在你声明它的地方初始化它......或者我对这最后一部分是错误的吗?

Level* level;

D3DMATRIX* matProjection,* matView;

//called once
void Initialise(HWND hWnd)
{
initD3D(hWnd);
    init_pipeline();

level = new Level;
}
Run Code Online (Sandbox Code Playgroud)

我是来自c#而在c#中,你只是在声明一个名字Level level; 是否参数,你仍然需要在某个时候初始化它.

c++

-1
推荐指数
1
解决办法
101
查看次数

如何创建一个以视图为参数的objective-c方法?

我有一些代码可以实例化很多东西.此代码需要添加到视图中.因此,我需要能够使用view参数调用函数,然后将我的代码设置为方法体中的该视图.

这样做有什么选择?我读过你可以使用代表吗?但不确定如何.

objective-c uiview ios

-1
推荐指数
1
解决办法
85
查看次数

为什么这个for循环需要这么长时间?

for循环:

//for testing, this is usually a value of about 27
int test = level.PathLookupVectors()[globalNodePositionIndex][globalNodeChoice].size();

for (int i = 0; i < level.PathLookupVectors()[globalNodePositionIndex][globalNodeChoice].size(); i++)
{
    //adds the correct nodes to the search
    search.push_back(level.Nodes()[level.PathLookupVectors()[globalNodePositionIndex][globalNodeChoice][i]].Index());
}
Run Code Online (Sandbox Code Playgroud)

它是一个64位系统.

我在调试时得到整数'i'的非常奇怪的结果.它应该初始化为0,但由于某种原因,它是一个非常高的数字,这反过来意味着for循环没有执行.

c++

-6
推荐指数
1
解决办法
186
查看次数

标签 统计

c++ ×6

constructor ×1

ios ×1

mysql ×1

objective-c ×1

performance ×1

php ×1

phpmyadmin ×1

pointers ×1

sql ×1

uiview ×1