gpt4 book ai didi

c++ - 我如何在 c++ 中重载按位 AND (&) 将左操作数 (const char []) 与右操作数字符串对象连接起来)(家庭作业)

转载 作者:太空狗 更新时间:2023-10-29 23:06:45 25 4
gpt4 key购买 nike

编辑:紧随其后的代码是工作版本,位于标题中

inline char * operator & (const char String1 [], const MyStringClass & String2)
{
int length = strlen (String1) + String2.Length();
char * pTemp = new char [length + 1];
strcpy (pTemp, String1);
strcat (pTemp, String2.GetStr());
return pTemp;
}

这是我第一次觉得有必要问一个问题,因为我自己无法找到有用的信息(通过搜索、谷歌、书籍等)。我的课本是 C++ Primer 第 5 版,我读过 Ch. 14 涵盖运算符重载。我不一定在寻找“答案”,而是在正确的方向上插入(因为我确实想学习这些东西)。

赋值让我们创建自己的字符串类并重载一堆运算符,这些运算符将在任一侧接受一个类对象——赋值运算符除外,它可能只在左侧接受一个类对象。我玩过各种返回类型(这不能是成员函数;使它成为友元函数的努力失败了)。

/* 
Note: return by value, otherwise I get a warning of returning the address
of a local variable, temporary. But no matter the return type or what I'm
returning, I always get the error: C2677: binary '&' : no global operator
found which takes type 'MyStringClass' (or there is no acceptable
conversion)
*/

MyStringClass operator & (const char String1 [], const MyStringClass & String2)
{
/*
The only requirement is that the left side has const char [] so that
(const char []) & (MyStringClass &) will concatenate. There is no return
type requirement; so, I could either try and return a string object or
an anonymous C-type string.

cout << StringOject1 << endl; // this works
cout << (StringObject1 & "bacon") << endl; // so does this;
// another function overloads & such that: obj & const char [] works

cout << ("bacon" & StringObject1) << endl; // but not this
*/

MyStringClass S (String1); // initialize a new object with String1
S.Concat (String2); // public member function Concat() concatenates String2
// onto String1 in S
return S; // this does not work

/* a different way of trying this... */
int Characters = strlen (String1) + String2.Length();
int Slots = Characters;
char * pTemp = new char [Slots + 1];
strcpy (pTemp, String1);
strcat (pTemp, String2.pString); // this won't work; pString is a private
// member holding char * and inaccessible
// making it pointless to try and initialize and return an object with pTemp
}

最佳答案

查看了你的代码,据我所知,你可能正在寻找这样的东西:

class MyStringClass
{
public:
const char* data() const;

private:
const char* charptr;
};


const char* MyStringClass::data() const
{
return charptr;
}


MyStringClass operator & (const char String1 [], const MyStringClass & String2)
{
/* a different way of trying this... */
int len = strlen(String1) + String2.Length();
char * pTemp = new char [len + 1]; //total length of both strings
strcpy (pTemp, String1);
strcat (pTemp, String2.data()); // you need to have a public member function that returns the string as const char*
MyStringClass str(pTemp); //requires MyStringClass to have constructor that takes char*
return str; //return the string

}

关于c++ - 我如何在 c++ 中重载按位 AND (&) 将左操作数 (const char []) 与右操作数字符串对象连接起来)(家庭作业),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com