push_back 是否更改存储在其向量中的类型的结构?
Does push_back change the structure of type stored in its vector?
我有以下 2 类:
class Core
{
public:
Core() : mid(0), fin(0) {}
Core(std::istream &in)
{
read(in);
}
virtual ~Core() {}
Str name() const
{
return n;
}
virtual double grade() const
{
return vec_grade(mid, fin, hws);
}
virtual std::istream &read(std::istream &in);
protected:
std::istream &read_common(std::istream &in);
double mid, fin;
Vec<double> hws;
virtual Core *clone() const
{
return new Core(*this);
}
private:
friend class Student2;
Str n;
};
std::istream &Core::read_common(std::istream &in)
{
in >> n >> mid >> fin;
return in;
}
std::istream &Core::read(std::istream &in)
{
read_common(in);
read_vec(in, hws);
return in;
}
和
class Student2
{
Core *cp;
public:
Student2() : cp(0) {}
Student2(std::istream &in) : cp(0)
{
read(in);
}
Student2(const Student2 &s) : cp(0)
{
if (s.cp)
s.cp->clone();
}
Student2 &operator=(const Student2 &s);
~Student2()
{
delete cp;
}
std::istream &read(std::istream &in);
Str name() const
{
if (cp)
return cp->name();
else
throw std::runtime_error("uninitialized Student");
}
double grade() const
{
if (cp)
return cp->grade();
else
throw std::runtime_error("uninitialized Student");
}
static bool compare(const Student2 &x, const Student2 &y)
{
return x.name() < y.name();
}
Core *getPointer() const
{
return cp;
}
};
std::istream &Student2::read(std::istream &in)
{
delete cp;
char ch;
in >> ch;
if (ch == 'U')
{
cp = new Core(in);
}
else
{
cp = new Grad(in);
}
return in;
}
现在的问题是:
int main()
{
vector<Student2> v;
Student2 s(cin);
v.push_back(s);
printf("%p\n%p\n", s.getPointer(), v[0].getPointer());
}
输出:
0x562fa03b9280
(nil)
在这里我读入对象 Student2
,对象将有它的地址。但是,一旦我通过 push_back
、 相同的 objetc 将其添加到 vector 中,它的地址就会以某种方式丢失(将变为空指针)。我不知道 push_back
如何修改它的值,但它不应该(因为否则它会获得它的值)。 push_back
的值是什么?
它使用你写的copy-constructor。这个:
Student2(const Student2 &s) : cp(0)
{
if (s.cp)
s.cp->clone();
}
将 cp
设置为空 (0)。这就是副本的 cp
为空的原因。
我有以下 2 类:
class Core
{
public:
Core() : mid(0), fin(0) {}
Core(std::istream &in)
{
read(in);
}
virtual ~Core() {}
Str name() const
{
return n;
}
virtual double grade() const
{
return vec_grade(mid, fin, hws);
}
virtual std::istream &read(std::istream &in);
protected:
std::istream &read_common(std::istream &in);
double mid, fin;
Vec<double> hws;
virtual Core *clone() const
{
return new Core(*this);
}
private:
friend class Student2;
Str n;
};
std::istream &Core::read_common(std::istream &in)
{
in >> n >> mid >> fin;
return in;
}
std::istream &Core::read(std::istream &in)
{
read_common(in);
read_vec(in, hws);
return in;
}
和
class Student2
{
Core *cp;
public:
Student2() : cp(0) {}
Student2(std::istream &in) : cp(0)
{
read(in);
}
Student2(const Student2 &s) : cp(0)
{
if (s.cp)
s.cp->clone();
}
Student2 &operator=(const Student2 &s);
~Student2()
{
delete cp;
}
std::istream &read(std::istream &in);
Str name() const
{
if (cp)
return cp->name();
else
throw std::runtime_error("uninitialized Student");
}
double grade() const
{
if (cp)
return cp->grade();
else
throw std::runtime_error("uninitialized Student");
}
static bool compare(const Student2 &x, const Student2 &y)
{
return x.name() < y.name();
}
Core *getPointer() const
{
return cp;
}
};
std::istream &Student2::read(std::istream &in)
{
delete cp;
char ch;
in >> ch;
if (ch == 'U')
{
cp = new Core(in);
}
else
{
cp = new Grad(in);
}
return in;
}
现在的问题是:
int main()
{
vector<Student2> v;
Student2 s(cin);
v.push_back(s);
printf("%p\n%p\n", s.getPointer(), v[0].getPointer());
}
输出:
0x562fa03b9280
(nil)
在这里我读入对象 Student2
,对象将有它的地址。但是,一旦我通过 push_back
、 相同的 objetc 将其添加到 vector 中,它的地址就会以某种方式丢失(将变为空指针)。我不知道 push_back
如何修改它的值,但它不应该(因为否则它会获得它的值)。 push_back
的值是什么?
它使用你写的copy-constructor。这个:
Student2(const Student2 &s) : cp(0)
{
if (s.cp)
s.cp->clone();
}
将 cp
设置为空 (0)。这就是副本的 cp
为空的原因。