将缓冲区作为参数的类函数,混淆了
class function with buffer as a parameter, confused
我有一个需要为学校完成的练习,这让我很困惑。我只需要一个正确方向的提示,因为这对我来说没有意义
向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来防止溢出
如果name和salary是类成员,那么您已经可以在成员函数中访问它们:无需传入它们。传递的buffer
参数就是您用函数填充的参数:Employee
类的用户将调用该函数,传入一个缓冲区以填充结果。
因此,您的任务是定义Employee
类API的一部分,提供类内部的特定表示,并了解缓冲区边界/字符串处理。
您的讲师要求的是一个可以如下使用的函数:
- 调用方分配一个缓冲区,不管它选择什么大小
- 调用方调用
Employee::format
,同时传递缓冲区和缓冲区大小 Employee::format
用员工姓名和工资填充缓冲区,确保它不会写过缓冲区的末尾- 调用方现在有一个Employee的字符串表示,它可以使用它来做任何需要做的事情(打印到控制台、保存到文件、在GUI小部件中显示等)
从本质上讲,缓冲区参数是一个返回值。Employee::format
的唯一职责是将名称和薪资写入缓冲区,供调用方使用。考虑到这一点,以下是您可能想要做的一些更改:
- 您在
Employee
中有一个buffer
成员,看起来它可能是为format
添加的以供使用。这是不必要的,因为调用者负责分配缓冲区。此外,format
中的buffer
参数隐藏了同名成员,因此无论如何都不会使用该成员 format
末尾的cout << buffer << endl;
行将员工信息打印到控制台,但调用者可能不希望这样。format
应该只将员工信息写入缓冲区
我猜你的老师要求你将姓名和工资信息格式化到缓冲区中,并确保格式化后的文本不会超过缓冲区的大小。
名称和薪资必须是员工类的成员变量。
- 如何通过派生类函数更改基类中的向量
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 在类函数中初始化外部作用域变量
- c++ 在非类函数中使用类变量
- SDL_PollEvent() 无法捕获类函数内部SDL_QUIT?
- 从类成员函数到类 C 函数指针的转换
- 如何在模板类函数中分配结构值?
- 有没有办法将重载的类函数绑定到函数对象?
- 启动类函数作为失去引用的线程
- C++调用使用重写函数的父类函数
- 将值传递到另一个类函数在打印时为零
- 计算对类函数的所有调用次数
- 由于签名差异,调用了错误的子类函数
- 使用宏调用类函数
- 虚拟基类函数中派生类的大小
- 模板类/函数中的交叉前向声明
- 在C++中使用非静态类函数的函数
- 无法访问基类函数 C++
- 类函数返回到另一个类函数返回打印不同的值
- 将缓冲区作为参数的类函数,混淆了