用缓冲区作为参数的类函数,很困惑

时间:2012-08-26 03:09:49

标签: c++ class function char

我需要为学校完成一项让我感到困惑的练习。我只需要在正确的方向上提示,因为这对我没有意义。

将成员函数添加到Employee类:

void Employee::format(char buffer[], int buffer_maxlength)

成员函数应该使用员工的姓名和薪水填充缓冲区。一定不要超过缓冲区。它可以保存buffer_maxlength个字符,不包括'\ 0'终止符。

我没有得到的是传递给函数的参数。不应该传递名称,然后输入缓冲区?或者,该函数不应该采取任何参数并填充缓冲区?如果缓冲区是参数,我该如何填充它?

在这里感到困惑。

尚未开始编码,因为我还不了解这项练习。

不需要任何人为我编写程序,只需要提示一下这里发生了什么。

感谢。

编辑: 这是我到目前为止的代码,它似乎工作。我不确定的一件事是缓冲区溢出。因为我无法调整缓冲区的大小,所以我只是将其设为一个我知道不能用现有数据溢出的大小?这似乎效率低下,但不确定还能做什么。

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string.h>
#pragma warning(disable : 4996) // had to include this for the strcpy function, not sure why

using namespace std;

/**
A basic employee class that is used in many examples
in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
   /**
   Constructs an employee with empty name and no salary.
   */
Employee();
/**
  Constructs an employee with a given name and salary.
  @param employee_name the employee name
  @param initial_salary the initial salary
*/
Employee(string employee_name, double initial_salary);
/**
  Sets the salary of this employee.
  @param new_salary the new salary value
*/
void set_salary(double new_salary);
/**
  Gets the salary of this employee.
  @return the current salary
*/
double get_salary() const;
/**
  Gets the name of this employee.
  @return the employee name
*/
string get_name() const;

void format(char buffer[], int buffer_maxlength);

private:
   string name;
   double salary;
   char buffer;
};

Employee::Employee()
{  
   salary = 0;
}

Employee::Employee(string employee_name, double initial_salary)
{  
   name = employee_name;
   salary = initial_salary;
}

void Employee::set_salary(double new_salary)
{  
   salary = new_salary;
}

double Employee::get_salary() const
{  
   return salary;
}

string Employee::get_name() const
{  
   return name;
}

void Employee::format(char buffer[], int buffer_maxlength)
{   
string temp_name;
//string space = " ";
char terminator = '\0';
double input_salary = salary;   
string s;   
stringstream output_salary;   
output_salary << input_salary;   
s = output_salary.str();
temp_name = name.c_str() + s + terminator;
strcpy(buffer, temp_name.c_str());
cout << buffer << endl; 
}


int main()
{
const int BUFFER_SIZE = 100;
char input_buffer[BUFFER_SIZE];

string temp_string;
string space = " ";

Employee bob_buffer("Buffer, Bob", 100000);
bob_buffer.format(input_buffer, BUFFER_SIZE);   

system("pause");

return 0;
}

编辑:使用strncpy而不是strcpy来防止溢出

3 个答案:

答案 0 :(得分:1)

如果 name salary 是类成员,那么您已经可以在成员函数中访问它们:无需传入它们。{{1传递的参数就是你正在填充的函数:你的buffer类的用户将调用该函数,传入一个缓冲区来填充结果。

因此,您的任务是定义Employee类API的一部分,提供类内部的特定表示,并了解缓冲区边界/字符串处理。

答案 1 :(得分:1)

您的教练要求的是一个可以按如下方式使用的功能:

  1. 调用者分配一个任意大小的缓冲区。
  2. 调用者调用Employee::format,传递缓冲区和缓冲区大小。
  3. Employee::format使用员工姓名和薪水填充缓冲区,确保它不会写入缓冲区的末尾。
  4. 调用者现在有一个Employee的字符串表示形式,它可以用于它需要做的任何事情(打印到控制台,保存到文件,显示在GUI小部件中等)。
  5. 基本上,buffer参数是返回值。 Employee::format的唯一责任是将名称和工资写入缓冲区供调用者使用。考虑到这一点,您可能需要进行一些更改:

    • buffer中有Employee成员,看起来可能已为format添加了buffer成员。这是不必要的,因为调用者负责分配缓冲区。此外,format中的cout << buffer << endl;参数隐藏了同名的成员,因此该成员永远不会被使用。
    • format末尾的format行会将员工信息打印到控制台,但调用者可能不希望这样。 {{1}}只应将员工信息写入缓冲区。

答案 2 :(得分:0)

我想你的老师要求你将名称和工资信息格式化到缓冲区中,并确保你的格式化文本不会超出缓冲区大小。

名称和工资必须是员工类的成员变量。