过于热心的方法无需调用即可完成工作? C++过多的初始化变得异常

发布于 2024-09-17 06:38:12 字数 2213 浏览 3 评论 0原文

菜鸟 C++ 程序员仍然在这里。

我正在使用 VC++ VS2008 编译器和 glut 库。一切都工作正常并且是最新的(我知道由于 XNA (C#) 支持原因,2010 年只有 cba)

好吧,这次我有一个与代码相关的问题,但我可以使代码工作。我不能做的是弄清楚幕后发生了什么。这就是我想弄清楚的。

假设一切正常,因为显然在运行时它可以正常工作!

这是代码(最小化到实际运行到其裸核心的代码,以帮助加快速度)

#include "stdafx.h"
#include "Controller.h"
#include "glut.h"
#include <iostream>

void main(int argc, char** argv) 
{   
    glutInit(&argc, argv);
    //controller_ptr->InitController();  <---- no link to controller-->DrawScene

    //INIT GLUT
    //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    //glutInitWindowSize(400, 400); //Set the window size
    //glutCreateWindow("Some Program"); 

    //INIT RENDERING
    glEnable(GL_DEPTH_TEST);

    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);
    glutMainLoop();
}

您可能会想,嘿,您已经注释了初始化代码。 我确实如此。你猜怎么着,它仍然有效!为什么?我真的不知道除了烧烤之外的过剩,所以是的...<----这可能是你调用失败的一个好点,如果有的话

我有一个带有此方法的绘图场景类

void DrawScene::initGlut()
{
    //Enables depth culling/front face culling??????  <----- what about that, some   depth thingy was involved is all I'm sure of
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400); //Set the window size
    glutCreateWindow("Some Program"); 
}

当我评论这个并取消注释时主要是相同的结果。我没有评论, glut 说 init 已经完成了两次,我也没有评论, init 没有完成,一切都很顺利。所以唯一合乎逻辑的结论就是调用这个方法,对吧?

还有很多其他方法和类,但没有 displayescene 类或控制器类的实例。我可以评论包含的内容,它也会做同样的事情。

在 MSbuild 或 VC++ 开发中是否有一些我不理解的设计部分,它允许方法沿着我不知道的某些流或流程进行筛选?据我所知,如果我不创建类的实例,它就不相关,至少它是非静态成员。那么,该成员中的代码处于活动或非活动状态如何对我的程序产生影响呢?

可能忘记了一些非常愚蠢的事情,但无论如何我宁愿尽快找到答案,作为新手的好处是我不在乎摔倒并再次尝试,所以请发表评论^^

谢谢非常提前!

编辑:什么?现在,没有对代码进行任何更改的控制台确实报告了一次对 init 的双重调用,其中主 init 位于注释中,而绘图场景中的 init 未位于注释中......我非常困惑。

编辑 2:14:45 29/08/2010

好的,我发现了一些东西....

drawscene 中的构造函数调用该类的 initGlut 和 initRendering 方法。控制器创建一个指向堆的指针,其中包含一个drawScene对象,这会导致方法运行初始化代码。

然而,控制器不包含在主程序中,也不被实例化。该drawScene代码在主程序启动之前运行,然后我可以得出结论,构建本身就是问题,所以我应该避免构造函数中的初始化代码吗?

编辑3:15:02 29/08/2010

发现另一个,当只有drawScene 是活动的初始化代码时,确实会发生双重调用。是否每次包含drawscene都会导致构造函数代码运行初始化代码并导致双重初始化?

rookie C++ programmer here still.

I'm using VC++ VS2008 compiler and glut library. All working fine and up to date ( I know there's 2010 just cba because of XNA (C#) support reasons)

Ok this time I have a question that is code related but I can make the code work. What I can not do is figure out what is happening under the hood. That's what I'd like to figure out.

Assume all works, because clearly in runtime it works!

Here's the code (minimized to what is actually running to it's bare core to help speed this up)

#include "stdafx.h"
#include "Controller.h"
#include "glut.h"
#include <iostream>

void main(int argc, char** argv) 
{   
    glutInit(&argc, argv);
    //controller_ptr->InitController();  <---- no link to controller-->DrawScene

    //INIT GLUT
    //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    //glutInitWindowSize(400, 400); //Set the window size
    //glutCreateWindow("Some Program"); 

    //INIT RENDERING
    glEnable(GL_DEPTH_TEST);

    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);
    glutMainLoop();
}

You may be thinking, hey you have the init code commented.
I do indeed. Guess what , it is still working! Why? I don't really KNOW glut except on BBQ's so ye.... <---- This could be a good point for you to call the fail if any

I have a drawscene class with this method

void DrawScene::initGlut()
{
    //Enables depth culling/front face culling??????  <----- what about that, some   depth thingy was involved is all I'm sure of
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400); //Set the window size
    glutCreateWindow("Some Program"); 
}

When I comment this and uncomment the one in the main, same result. I comment none, glut says init is done twice, I comment neither, init is not done it all goes boom. So the only logical conclusion would be to say this method is called, right?

There's a bunch of other methods around and classes but there is no instance of the displayescene class nor controller class. I can comment the includes and it will do all the same.

Is there some design part I do not understand in MSbuild or VC++ development which allows methods to sift along some stream or flow that I am unaware of? As far as my knowledge goes, if I do not make an instance of a class it's not relevant, at least it's non-static members. So how can this code being active or inactive in this member have an effect on my program then?

Probably forgetting something incredibly stupid, but I'd rather find out a.s.a.p. anyway, the good part about being a rookie is that I couldn't care less about falling flat on my face and trying again so fire away with the comments ^^

Thank you very much in advance!

EDIT: What the ? Now the console without any change to the code did report once a double call to init with the main init in comment and the one in drawscene not in comment...I am very much confused.

EDIT 2: 14:45 29/08/2010

Ok I found something....

The constructor in drawscene calls the initGlut and initRendering methods of the class. The controller makes a pointer to the heap holding a drawScene object there which causes the methods to run the init code.

The controller however is not included in the main nor is it instantiated. That drawScene code is run before the main starts and can I then conclude that the build itself is the problem so I should avoid init code in constructors?

EDIT 3: 15:02 29/08/2010

Found another , the double call does occur when only the drawScene is the active init code. Could it be that every include of drawscene causes constructor code to run init code and cause the double init?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-09-24 06:38:12

为什么不在初始化代码上放置一个断点,然后沿着调用堆栈查看它被调用的位置?

慢慢地逐步完成它,我想你会找到源代码。

编辑:将库之类的东西的初始化保留在构造函数之外是很好的做法。就我个人而言,我只会测试 glut 是否已在构造函数中初始化(如果需要),如果未检测到,则抛出异常。

Why not stick a break point on your init code and then walk up the call stack to see where it's being called?

Step through it slowly and I would imagine you'll find the source..

Edit: keeping initialisation of things like libraries out of the constructor is good practice. Personally, I would just test that glut has been initialised in the constructor if it's required and throw an exception if it's not detected.

落在眉间の轻吻 2024-09-24 06:38:12

显然,那些漂亮的指针不是对象的一部分,而是全局变量,GG C# VS C++ :P

cpp 文件中的全局声明不好,吸取教训

apperantly those nifty pointers were not part of an object but globals, GG C# VS C++ :P

Global declaration of stuff in cpp files BAD , lesson learned

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文