将缓冲区作为参数的类函数,混淆了

class function with buffer as a parameter, confused

本文关键字:类函数 缓冲区 参数      更新时间:2023-10-16

我有一个需要为学校完成的练习,这让我很困惑。我只需要一个正确方向的提示,因为这对我来说没有意义

向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 = '';
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;
}

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

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

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

您的讲师要求的是一个可以如下使用的函数:

  1. 调用方分配一个缓冲区,不管它选择什么大小
  2. 调用方调用Employee::format,同时传递缓冲区和缓冲区大小
  3. Employee::format用员工姓名和工资填充缓冲区,确保它不会写过缓冲区的末尾
  4. 调用方现在有一个Employee的字符串表示,它可以使用它来做任何需要做的事情(打印到控制台、保存到文件、在GUI小部件中显示等)

从本质上讲,缓冲区参数是一个返回值。Employee::format的唯一职责是将名称和薪资写入缓冲区,供调用方使用。考虑到这一点,以下是您可能想要做的一些更改:

  • 您在Employee中有一个buffer成员,看起来它可能是为format添加的以供使用。这是不必要的,因为调用者负责分配缓冲区。此外,format中的buffer参数隐藏了同名成员,因此无论如何都不会使用该成员
  • format末尾的cout << buffer << endl;行将员工信息打印到控制台,但调用者可能不希望这样。format应该只将员工信息写入缓冲区

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

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