简体   繁体   English

做一些计数器操作,我该如何做,如果 setCounter() 没有 arguments,计数器将为零?

[英]Doing some counter manipulation, how do I make it so that if setCounter() has no arguments, the counter would be zero?

Given the following C++ program:给定以下 C++ 程序:

#include "counterType.h"
#include <iostream>
#include <string>

using namespace std;

counterType::counterType()
{
    counter = 0;
}

counterType::counterType(int c)
{
    setCounter(c);
}

void counterType::setCounter(int ck)
{
    counter = ck;
}

int counterType::getCounter()
{
    return counter;
}

void counterType::incrementCounter()
{
    ++counter;
}

void counterType::decrementCounter()
{
    --counter;
}

void counterType::print()
{
    cout << "Counter = "<< counter << endl;
}

The code seems to only work when setCounter() has an argument in it.该代码似乎仅在 setCounter() 中有参数时才有效。 The only tests that fail are when is has no argument.唯一失败的测试是当 is 没有参数时。 So how do I check it in a way that if there is no argument, then the counter would be 0?那么我如何检查它,如果没有参数,那么计数器将为 0?

This is the perfect place for a default function argument.这是默认 function 参数的理想位置。 Since this is a class member function that means you need to change your function declaration to be由于这是一个 class 成员 function 这意味着您需要将您的 function 声明更改为

void setCounter(int ck = 0);

To tell the compiler that if a value is not provided for ck that it can use 0 as the default value.告诉编译器如果没有为ck提供值,它可以使用0作为默认值。 That means your function definition stays the same since it "pulls in" the default value from he declaration.这意味着您的 function 定义保持不变,因为它从声明中“拉入”了默认值。

Two approaches:两种方法:

1) Supply a default argument in the function declaration so you can call the function without supplying it. 1)在 function 声明中提供一个默认参数,这样您就可以在不提供它的情况下调用 function。

void setCounter(int ck = 0);

2) Overload the function 2)重载function

void counterType::setCounter()
{
    counter = 0;
}

Some software houses (particularly those where the code standard guys are Java folk) disallow the first style.一些软件公司(尤其是那些代码标准是 Java 的人)不允许第一种样式。 A less facetious reason for not liking the first way is that the default argument value can be run-time evaluable (a member variable perhaps) and that can harm program stability.不喜欢第一种方式的一个不那么滑稽的原因是默认参数值可以是运行时可评估的(可能是成员变量)并且会损害程序的稳定性。

For starters you can initialize the data member counter in the declaration of the data member within the class definition.对于初学者,您可以在 class 定义中的数据成员声明中初始化数据成员counter For example例如

class counterType
{
    //...
    int counter = 0;
};

In this case the constructors can look like在这种情况下,构造函数看起来像

counterType::counterType()
{
}

counterType::counterType(int c) : counter( c )
{
}

Or you could define the constructors the following way或者您可以通过以下方式定义构造函数

counterType::counterType() : counterType( 0 ) 
{
}

counterType::counterType(int c) : counter( c )
{
}

The function setCounter can have a default argument function setCounter可以有一个默认参数

void counterType::setCounter(int ck = 0 )
{
    counter = ck;
}

It is better to declare the function getCounter within the class definition like最好在 class 定义中声明 function getCounter

int getCounter() const;

because the function does not change the object itself.因为 function 不会改变 object 本身。 And define it like并将其定义为

int counterType::getCounter() const
{
    return counter;
}

And this function还有这个 function

void counterType::print()
{
    cout << "Counter = "<< counter << endl;
}

is better to declare and define like最好像这样声明和定义

std::ostream & print( std::ostream &os = std::cout ) const;

and define it like并将其定义为

std::ostream & print( std::ostream &os = std::cout ) const;
{
    return os << "Counter = "<< counter << endl;
}

It also does not change the object itself so it should be declared with the qualifier const .它也不会更改 object 本身,因此应使用限定符const声明它。

As it seems your class has only one data member then you could define the operator << for the whole class.看起来您的 class 只有一个数据成员,然后您可以为整个 class 定义operator << For example例如

class counterType
{
    //...
    friend std::ostream & operator <<( std::ostream &os, const counterType &c );
};

std::ostream & operator <<( std::ostream &os, const counterType &c )
{
    return os << "Counter = "<< c.counter << endl;
}

Do you mean something like that?你的意思是这样的吗?

void counterType::setCounter()
{
    counter = 0;
}

What you're trying to achieve is called function overloading : given a different list of arguments, C++ lets you define a different "version" of the same function. What you're trying to achieve is called function overloading : given a different list of arguments, C++ lets you define a different "version" of the same function.

void counterType::setCounter()           // No argument in there
{
    counter = 0;
}

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

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