将类构造函数重载放入同一类的另一个重载中

发布于 2024-11-07 09:06:13 字数 2148 浏览 5 评论 0原文

嘿,我正在使用 windows GetTickCount() 和 STL 开发一个秒表类,但是遇到了一个问题,因为在重载 Stopwatch(int DecNumb, char command[]) 中实现 Stopwatch(int DecNumb) 构造函数时, “accuracy”数据类型在后一个构造函数中未正确设置。

(它似乎返回到 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;
    }

为什么准确性不停留在该值我也在构造函数中设置了它? 谢谢! =) 哦,欢迎对我的代码提供任何其他反馈! (我比较新)

Hey so i'm working on a stopwatch class using windows GetTickCount() and the STL, but have run into a problem in that when implementing the Stopwatch(int DecNumb) constructor into the overload Stopwatch(int DecNumb, char command[]) the "accuracy" data type is not set correctly in the latter constructor.

(it seems to return to the former value of the unsigned long int 560345 or something...)

Here's the class and main() commands i'm using to test it:

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;};

    };

And here's main()

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

Why is accuracy not staying at the value i set it too in the constructor? Thanks! =)
oh and any other feedback on my code is welcome! (i'm rather new)

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

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

发布评论

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

评论(2

童话 2024-11-14 09:06:13
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();
    }
}

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

Stopwatch::Stopwatch(aDecNumb);

This does not call the other constructor; it isn't even valid C++.

You can't call one constructor from another constructor for the same object. Only one constructor is called during the creation of an object (not including base class or member constructors, of course).

You don't really need two constructors here, though; one will suffice:

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

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

If you really do need two constructors, your best bet is either to use a base class that performs initialization that is common to multiple constructors or to use an "initialize" member function that can be called from the multiple constructors.

画离情绘悲伤 2024-11-14 09:06:13

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

//秒表::秒表(aDecNumb);用于在类定义之外声明构造函数,这里这种用法是错误的,它不会产生任何东西。

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

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

//Stopwatch::Stopwatch(aDecNumb); is used to declare a constructor outside the class definition, here this utilisation is wrong, it does't make anything.

You could build a class and use it as interface/ or as a Base Class add derive from it if you really want to initialise the constructors.

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