简体   繁体   English

构造函数和析构函数 - c ++

[英]constructors and destructors - c++

I need to write a program that prints 100 stars on the screen (at random places), and then the stars disappear slowly - one after another. 我需要编写一个程序,在屏幕上打印100颗星(在随机的地方),然后星星一个接一个地慢慢消失。 I'm not allowed to use loops nor recursions. 我不允许使用循环或递归。 I've tried to play with the constructors and the destructors but I can't get the stars to disappear one after another (and not all together). 我试图与构造函数和析构函数一起玩,但我不能让星星一个接一个地消失(而不是一起消失)。 Any ideas? 有任何想法吗?

Thanks, Li 谢谢,李

Sorry - forgot to mention i'm using c++ 对不起 - 忘了提我正在使用c ++

My current access violating useless code: 我目前的访问违反了无用的代码:

class star {
    int x;
    int y;
public:
    star(){
        x = rand()%80;
        y = rand()%80;
        PaintcharOnRandomLocation('*',x,y);
    };
    ~star(){
        PaintcharOnRandomLocation(' ',x,y);
    };

};

class printAll{
    star* arr;
public:
    printAll(){
    arr = new star[100];
    };


    ~printAll(){
        delete[] arr;
    };


};
void doNothing(printAll L){
};

void main()
{
    srand ( time(NULL) );   
    doNothing(printAll());

     getch();
};

Seems the only way possible without loops/recursion is something like this: 似乎没有循环/递归的唯一方法是这样的:

class Star
{
  Star() 
  { 
     //constructor shows star in a a random place
  }
  ~Star()
  {
    //destructor removes star and sleeps for a random amount of time
  }
};

int main() 
{
   Star S[100];
}

This is really just a dumb trick because the compiler has to run the constructor for each star to initialise the array and then the destructor for EACH star as it goes out of scope. 这实际上只是一个愚蠢的技巧,因为编译器必须运行每个星的构造函数来初始化数组,然后运行EACH star的析构函数,因为它超出了范围。

It is also a bad trick as all the workings that go in the main function are opaque and invisible. 这也是一个糟糕的伎俩,因为主要功能中的所有工作都是不透明和不可见的。 It would obviously be better to use a loop in this context and putting the delay inside a destructor like this is really confusing and unmaintainable. 在这种情况下使用循环显然会更好,并且在这样的析构函数中放置延迟实际上是令人困惑和不可维护的。

This is not a runtime recursion: 这不是运行时递归:

template<int N>
struct Star
{
   Star() { DrawAtRandomPlace(); }
   ~Star() { RemoveSlowly(); }
   Star<N-1> star;
};

template<> struct Star<0> {};

int main()
{
  Star<100> stars;
}

The code above will generate 100 different instantiations of the Star template. 上面的代码将生成100个不同的Star模板实例。 RAII will guarantee the order of drawing and removing. RAII将保证绘图和删除的顺序。

Based on your final comment, can you have the destructor of your star class do a delay? 根据你的最终评论,你可以让你的star的析构函数延迟吗? See for example the sleep or usleep functions. 见例如sleepusleep功能。

Since Destructors/Constructors are only an Idea, they're probably not the right title for your question. 由于Destructors / Constructors只是一个想法,它们可能不是你问题的正确标题。 I don't know what system/environment you are in, but how about this: 我不知道你在哪个系统/环境,但是这个怎么样:

Create a buffer that contains a string with your stars, simply manually by typing them in the code. 创建一个包含星形字符串的缓冲区,只需在代码中键入它们即可手动创建。

Next, write a function that displays the buffer to whatever output window you use. 接下来,编写一个函数,将缓冲区显示到您使用的任何输出窗口。

Then, you would need a function that has a static(!) pointer to the back of the buffer, and that does the following: 然后,您需要一个具有指向缓冲区后面的静态(!)指针的函数,并执行以下操作:

  • Call the buffer printing function 调用缓冲打印功能
  • Write a null byte under the current pointer position 在当前指针位置下写一个空字节
  • Decrement the static pointer 减少静态指针
  • Wait for a period of time 等一段时间
  • Raise a custom signal with raise() 使用raise()引发自定义信号

In the main() function, you set the the Signal Handler for your custom signal to the function described above, and then you raise the custom signal. 在main()函数中,将自定义信号的信号处理程序设置为上述功能,然后引发自定义信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM