在C ++的void函数中可以有很多参数吗?

时间:2019-01-18 13:15:53

标签: c++ function global-variables void local-variables

我是一个初学者,所以请保持友善:)

我已经使用全局变量使程序正常工作。但是我想尝试使用局部变量,因为全局变量看起来很凌乱,而且我还发现始终使用它是一种不好的做法。该程序与局部变量一起运行,但无法正常运行。我在void函数funcDataSummary下显示结果时遇到问题。由用户输入的void funcDataSummary起作用并且(float numberOfRooms,float wallSpace,float costOfPaint ... ,则float totalCost)应为0。

使用全局变量的输出: Output with the use of Global Variables

使用局部变量的输出: output with the use of Local Variables

#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

// function prototype
void funcDataSummary(float, float, float, float, float, float, float, float);

// void function called in int main()
funcDataSummary(numberOfRooms, wallSpace, costOfPaint, gallonsOfPaint, totalCostOfPaint, hoursOfLabor, laborCost, totalCost);

// void function
void funcDataSummary(float numberOfRooms, float wallSpace, float costOfPaint, float gallonsOfPaint, float totalCostOfPaint, float hoursOfLabor, float laborCost, float totalCost)
{
    cout << "DETAILS" << endl;
    cout << "Number of Rooms = " << funcNumberOfRooms(numberOfRooms) << endl;
    cout << "Wall Dimension = " << funcWallSpace(wallSpace) << " square feet" << endl;
    cout << "Paint Cost = Php " << funcCostOfPaint(costOfPaint) << endl;
    cout << "Gallons of Paint = " << funcGallonsOfPaint(gallonsOfPaint);
    // singular and plural forms of units
    if(funcGallonsOfPaint(gallonsOfPaint) > 1)
    {
        cout << " Gallons" << endl;
    }
    else 
    {
        cout << " Gallon" << endl;
    }    
    cout << "Total Paint Cost = Php " << << funcTotalCostOfPaint(totalCostOfPaint) << endl;
    cout << "Labor hours = " << funcHoursOfLabor(hoursOfLabor);
    // singular and plural forms of units
    if(funcHoursOfLabor(hoursOfLabor) > 1)
    {
    cout << " hours" << endl;
    }
    else 
    {
        cout << " hour" << endl;
    }
    cout << "Labor Cost = Php " << funcLaborCost(laborCost) << endl; 
    cout << "TOTAL COST = Php " << funcTotalCost(totalCost) << endl;
}

2 个答案:

答案 0 :(得分:1)

虽然可以,但是会使您的代码有点复杂。 如果可能的话,最好将一个或多个structclass中彼此关联的变量进行分组,然后将这些对象(或指针/引用/常量引用(如果需要))作为参数传递。

答案 1 :(得分:0)

我的建议是使用不可变类存储函数中所需的所有东西,并将其作为const引用或指针传递。这是一个示例:

#include <iostream>
#include <string>

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

class ParamsHolder : private boost::noncopyable
{
private :
    std::string m_name;
    std::uint32_t m_value;

public :
    typedef boost::shared_ptr<ParamsHolder> pointer;

    ParamsHolder( const std::string name, std::uint32_t value )
    : m_name( name ), m_value( value ) {}

    virtual ~ParamsHolder() {}

    std::string getHolderName() const { return m_name; }
    std::uint32_t getHolderValue() const { return m_value; }
};

void testFunction( ParamsHolder::pointer holder )
{
    std::cout << holder->getHolderName() << std::endl;
    std::cout << holder->getHolderValue() << std::endl;
}

int main()
{
    std::cout << "Test program for holder" << std::endl;

    std::string testName = "some name";
    std::uint32_t testValue = 111;

    auto holder = boost::make_shared<ParamsHolder>( testName, testValue );
    testFunction( holder );

    return 0;
}

要创建具有许多参数的支架,请查看构建器设计模式或抽象工厂。