Putting a class constructor overload INSIDE another overload of the same class
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*/;开发者_JAVA百科};
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)
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.
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.
精彩评论