我编写这段代码只是为了练习来测试复制构造函数和析构函数的概念,但是它向我展示了一个错误,即共享指针不是模板,而且我的大部分指针都是根据编译器未定义的。
我想知道什么是错的。
class MyString
{
public:
MyString(); // default ctor
MyString(string const &); // ctor with ordinary string argument
MyString(MyString const &); // copy ctor
MyString(MyString &&); // move ctor – discussed later
~MyString(); // destructor
MyString& operator=(MyString const &rhsObject); // assignment operator
void showString(); // outputs the String in the object
private:
shared_ptr<string> rep;
}; // end class MyString
MyString::MyString()
{
// creates an object with empty string
rep = make_shared<string>();
}
MyString::MyString(string str)
{
// creates an object with an ordinary string pointed to
rep = make_shared<string>(str);
}
MyString::MyString(MyString const &ob)
{
// creates an object with String as in ob
rep = make_shared<string>(ob.rep);
}
MyString::~MyString()
{
}
MyString & MyString::operator=(const MyString &ob)
{
// resets the value of calling object to the value of ob
rep = make_shared<string>(ob.rep);
return *this; // “this” pointer explained below
}
void MyString::showString()
{
// outputs the string pointed to by rep
cout << *rep;
}
void main()
{
MyString a("Hello"), b = "CS 570", c("Students");
// constructor MyString(char *) creates objects a, b, and c
MyString d; // default constructor MyString() creates d
MyString e(a), f = c;
// copy constructor MyString(MyString &) creates objects e and f
cout << "String in object a = ";
a.showString();
cout << endl; cout << "String in object b = "; b.showString(); cout << endl;
cout << "String in object c = ";
c.showString(); cout << endl;
cout << "String in object d = ";
d.showString(); cout << endl;
cout << "String in object e = ";
e.showString(); cout << endl;
cout << "String in object f = ";
f.showString(); cout << endl;
d = c;
cout << "Object d reset to have String of c : "; d.showString(); cout << endl;
c = a;
cout << "Object c reset to have String of a : "; c.showString();
cout << endl;
//Copy constructor and this pointer 7
cout << "Object d’s string remains as before : "; d.showString();
cout << endl;
} // end main function
答案 0 :(得分:-1)
#include <string>
#include <memory>
#include <iostream>
class MyString
{
public:
MyString();
MyString(std::string str1);
MyString(MyString const &);
MyString(MyString &&);
~MyString();
MyString& operator=(MyString const &rhsObject);
std::string showString();
private:
static std::shared_ptr<std::string> rep;
};
MyString::MyString()
{
rep = std::make_shared<std::string>();
}
MyString::MyString(std::string str1)
{
rep = std::make_shared<std::string>(str1);
}
MyString::MyString(MyString const &ob)
{
rep = std::make_shared<std::string>(ob.rep);
}
MyString::~MyString()
{
}
MyString & MyString::operator=(const MyString &ob)
{
rep = std::make_shared<std::string>(ob.rep);
return *this;
}
std::string MyString::showString()
{
return *rep;
}
int main()
{
MyString a("Hello"),
b("CS 570"),
c("Students");
MyString d;
MyString e(a), f = c;
std::cout << a.showString() << std::endl;
std::cout << "\n" << a.showString() << std::endl;
std::cout << "String in object b = " << b.showString() << std::endl;
std::cout << "String in object c = ";
std::cout << c.showString() << std::endl;
std::cout << "String in object d = ";
std::cout << d.showString() << std::endl;
std::cout << "String in object e = ";
std::cout << e.showString() << std::endl;
std::cout << "String in object f = ";
std::cout << f.showString() << std::endl;
d = c;
std::cout << "Object d reset to have String of c : " << d.showString() << std::endl;
c = a;
std::cout << "Object c reset to have String of a : " << c.showString() << std::endl;
std::cout << "Object d’s string remains as before : " << d.showString() << std::endl;
return 0;
}
好吧这是我上一个例子的更新,我重新编写了你的所有代码并将100多个错误归结为1个错误,我故意留下了这个错误,因为这是你将遇到的一些事情并且必须要学习自己绕行。
我建议尽管这个很小,但要把它分解为你的主要来源,你的类的标题,以及你的函数来源,以便你更容易调试你的东西。
至于剩下的问题,你为什么要使用所有这些构造函数?
你可以使用1个构造函数(2甚至3)进行任何操作,其余的可以是函数。
你问出了什么问题,答案就是你所做的大部分代码都是重写并解决了一些最小的问题,这些问题可以让你进步并学会修复自己的代码。