【问题标题】:Putting a class constructor overload INSIDE another overload of the same class将类构造函数重载放在同一类的另一个重载中
【发布时间】:2011-08-23 20:38:20
【问题描述】:

嘿,所以我正在使用 windows GetTickCount() 和 STL 处理秒表类,但是在将 Stopwatch(int DecNumb) 构造函数实现到重载 Stopwatch(int DecNumb, char command[ ]) 在后一个构造函数中未正确设置“准确性”数据类型。

(好像又恢复到以前的unsigned long int 560345什么的值了……)

这是我用来测试它的 class 和 main() 命令:

class Stopwatch
    {
    protected:
        int               accuracy;
        unsigned long int initial_ilu;
        unsigned long int current_ilu;
        long float        output_fl;
       vector<long float> times;

    public:
        Stopwatch(int DecNumb) { // so accuracy*10 isn't 0
                                    accuracy = 1;
                                for(;DecNumb>0;DecNumb--) // the Tick count will
                                accuracy = accuracy*10;}; // diveded by accuracy (each 0 in this number moves the decimal over once)
        Stopwatch(int aDecNumb, char command[]) {Stopwatch::Stopwatch(aDecNumb);
                                                 if(command = "start") Stopwatch::Start();}; 
        void Start(){initial_ilu = GetTickCount()/*/accuracy*/;};
        long float ElapsedTime()
        {
            current_ilu = GetTickCount()/*/accuracy*/;
            output_fl =  (current_ilu - initial_ilu)/accuracy;
            return output_fl;
        };
        void Wait(long float seconds) 
        {
            for(unsigned long int waitTime = GetTickCount() + (seconds*accuracy);
                waitTime > GetTickCount();) {}
            // stay stuck in for loop until time specified is up
        };

        void SaveTime(){times.push_back(GetTickCount()/*/accuracy*/);};
        long float GetTime(int location){if(times.size()<location+1) return times[location+1];
                                         else return -1;};

    };

这里是 main()

int main()
    {
        Stopwatch myStopwatch(3,"start");
        for(;;)
        {
            myStopwatch.Wait(2);
            cout << myStopwatch.ElapsedTime() << endl;
        }
                return 0;
    }

为什么精度不能保持在我在构造函数中设置的值? 谢谢! =) 哦,欢迎对我的代码提供任何其他反馈! (我比较新)

【问题讨论】:

  • 为什么要长时间浮动?你的意思是双倍的?受保护的数据通常是一件坏事。
  • 既然您要求其他反馈,除非代码对性能至关重要或需要与遗留代码互操作,否则确实没有理由使用 C 字符串。最好尽可能使用std::string(或其他字符串类,如果您使用的是其他框架),例如,用于构造函数的command 参数。
  • 我猜应该是if (command == "start") 而不是if (command = "start")。请注意,这只是指针比较,不是内容比较。
  • 你确实可以调用另一个构造函数,但不能不使用临时:*this = StopWatch(aDecNumb); 应该可以工作
  • StopWatch::Wait() 是一个繁忙的循环,会疯狂地消耗 CPU 周期,直到时间过去。请考虑暂停当前进程的其他等待机制。

标签: c++ time constructor stopwatch


【解决方案1】:
Stopwatch::Stopwatch(aDecNumb);

这不会调用其他构造函数;它甚至不是有效的 C++。

对于同一个对象,您不能从另一个构造函数调用一个构造函数。在创建对象的过程中只调用了一个构造函数(当然不包括基类或成员构造函数)。

不过,这里实际上并不需要两个构造函数;一个就足够了:

Stopwatch(int DecNumb, char* command = 0) {
    accuracy = 1;
    for(; DecNumb > 0; DecNumb--) {
        accuracy = accuracy * 10;
    }

    if (command && std::string(command) == "start") {
        Start();
    }
}

如果您确实需要两个构造函数,最好的选择是使用执行多个构造函数通用的初始化的基类,或者使用可以从多个构造函数调用的“初始化”成员函数。

【讨论】:

    【解决方案2】:

    秒表(int aDecNumb, char command[]) {秒表::秒表(aDecNumb); if(command = "start") 秒表::Start();};

    //秒表::秒表(aDecNumb);用于在类定义之外声明一个构造函数,这里这个使用是错误的,它什么也做不了。

    如果您真的想初始化构造函数,您可以构建一个类并将其用作接口/或作为基类添加派生。

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 2010-11-02
      • 2016-07-03
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      相关资源
      最近更新 更多