C++ 构造没有std::string的字符串

C++ 构造没有std::string的字符串,c++,c-strings,C++,C Strings,我正在从事一个项目,我们根本不允许使用库-我们只能使用字符串作为字符指针,我们必须为它们编写自己的函数(strcpy、strlen等)。我正在尝试使用以下头文件构造RentalCar类: #ifndef RENTALCAR_H #define RENTALCAR_H class RentalCar { public: RentalCar(); RentalCar(char* make, char* model); char* getMake() const; char* get

我正在从事一个项目,我们根本不允许使用
库-我们只能使用字符串作为字符指针,我们必须为它们编写自己的函数(strcpy、strlen等)。我正在尝试使用以下头文件构造RentalCar类:

#ifndef RENTALCAR_H
#define RENTALCAR_H
class RentalCar {
 public:
  RentalCar();
  RentalCar(char* make, char* model);
  char* getMake() const;
  char* getModel() const;
  void setMake(char* make = "");
  void setModel(char* model = "");
 private:
  char m_make[256];
  char m_model[256];
};
#endif
我的源文件包含以下内容:

#include <iostream>
#include "RentalCar.h"
using namespace std;

RentalCar::RentalCar() {
    setYear();
    setMake();
    setModel();
    setPrice();
    setAvailable();
}

RentalCar::RentalCar(int year, char* make, char* model, float price, 
bool available) {
    setYear(year);
    setMake(make);
    setModel(model);
    setPrice(price);
    setAvailable(available);
}

char* RentalCar::getMake() const{
    return m_make;
}

char* RentalCar::getModel() const{
    return m_model;
}

void RentalCar::setMake(char* make) {
    myStringCopy(m_make, make);
}

void RentalCar::setModel(char* model) {
    myStringCopy(m_model, model);
}


char* myStringCopy(char* destination, const char* source) {
    int index = 0;
    while(*(source + index) != '\0') {
        *(destination + index) = *(source + index);
        index++;
    }
    *(destination + index) = '\0';
    return destination;
}
我不知道如何在不使用文本的情况下构造默认字符串——这就是为什么我认为我会遇到这个错误

我的另一个问题是,为了在setMake()和setModel()函数中设置字符串,我需要使用myStringCopy()函数,所以我应该将它作为函数包含在这个类中,还是有其他方法可以访问它?我还需要在我的实际项目文件中使用它,并且在那里和RentalCar.cpp中包含它感觉是多余的

值得一提的是,我们不允许以任何方式使用数组索引处理字符串——除了初始化新字符串

任何帮助都将不胜感激!谢谢大家!

char* getMake() const;
char* getModel() const;
表示即使类是不可变的,也可以返回指向可变值的指针。函数声明后面的
const
表示当类作为一个整体为const时,该函数必须工作,这意味着所有*成员都选择
const
关键字

const char* getMake() const { return m_make; }
const char* getModel() const { return m_model; }
char* getMake() { return m_make; } 
char* getModel(){ return m_model; }
应该有用
const
具有不可变值的类的版本将获得不可变值,但非
const
不会。尽管如此,返回非
const
指针会破坏封装。所以我只想:

const char* getMake() const { return m_make; }
const char* getModel() const { return m_model; }
就这样吧。类的可变版本和不可变版本都将从get函数中获取不可变值。这可能是理想的结果


*
mutable
说“你好”,然后溜到角落里去死。

KitsuneYMG回答了你的编译问题。我想进一步谈谈你的代码

首先,这段代码更简单

char* myStringCopy(char* destination, const char* source) {
    char * retVal = destination;
    do {
        *(destination++) = *(source++);
    } while ( *(source++) != 0 );
    return retVal;
}
但如果您想使用代码,这也更容易理解:

char* myStringCopy(char* destination, const char* source) {
    int index = 0;
    while(source[index] != '\0') {
        destination[index] = source[index];
        index++;
    }
    destination[index] = '\0';
    return destination;
}
但这是一种可爱的方式来扭转局面:

char* myStringCopy(char* destination, const char* source) {
    int index = 0;
    do {
        destination[index] = source[index];
    } while (source[index++]);

    return destination;
}
下一个。如果你想成为一个真正的程序,这一点很重要。固定长度缓冲区是一个非常糟糕的主意,尤其是如果您不检查输入字符串的长度。如果您的数据包含256个字符(或更多)的字符串,那么您的256个字节将无法容纳该字符串加上0字节,并且您将发生数据损坏

在依赖固定长度缓冲区的代码中,这是一个非常常见的问题。这被称为缓冲区溢出,是黑客用来破坏软件的最大方法之一。巨大的安全问题


如果要使用固定长度的缓冲区,而不是学习如何使用new[]和delete[],则需要检查setters的输入长度。

为了解决这个问题,我将编写自己的字符串类。它不需要像
std::string
那样广泛,但是封装字符串管理和比较使使用它的代码更容易编写。@BRetnik这真是个麻烦事。很遗憾,很多类在教C++时都没有教C++,而BRTENK——它们必须被存储为char [],而不是字符串对象——这样你就可以在创建一个字符串类时得到更多的分数了吗?你要教的是如何轻松地创建缓冲区超支,从而不安全的代码-一个好的C++课程不应该教的东西。我敢打赌,在这门课之后,很多学生现在都会使用java、python或C++,永远放弃C++。鉴于这一天和年龄,我很惊讶,没有很多老师会说“这真的是教这样的学生很糟糕”,因此(至少)引发了他们的同龄人的讨论。你如何解释C++老师的被动语态?你被教垃圾,除非有一个押韵或理由教你“坏代码”,你会得到比较好的书面代码稍后。@ MikBurkand授予,但C++标准化20年前,因此<代码> STD::String < /代码>从那时起就可用。现在已经是一代人了。你能想象一个电视维修课程教你如何更换真空管吗?
char* myStringCopy(char* destination, const char* source) {
    int index = 0;
    do {
        destination[index] = source[index];
    } while (source[index++]);

    return destination;
}