为什么在push_back()之后,父母在向量中丢失的成员值? (C++)
我想使用push_back()在向量中推动孩子的实例(父母是子类的基础类)。但是随后,父母的价值损失了。 在下面的示例中,我想在将矢量写入控制台时看到70,但是我会得到随机值。是什么原因造成的?
main.cpp:
void demo(vector<Child> &ch) {
Child c(4);
c.setPrice(70);
cout << c.getPrice() << endl; // 70
ch.push_back(c);
cout << ch[ch.size()-1].getPrice() << endl; // random value
}
int main()
{
vector<Child> ch;
demo(ch);
return 0;
}
child.h:
#ifndef CHILD_H_INCLUDED
#define CHILD_H_INCLUDED
#include "Parent.h"
class Child : public Parent
{
private:
int siz;
public:
Child();
~Child();
Child(int);
Child(const Child&);
int getSiz() const;
void setSiz(int);
};
#endif // CHILD_H_INCLUDED
child.cpp:
#include "Child.h"
Child::Child()
{
}
Child::Child(int siz)
{
this->siz = siz;
}
Child::Child(const Child& other)
{
siz = other.siz;
}
Child::~Child () {
}
int Child::getSiz() const
{
return siz;
}
void Child::setSiz(int s)
{
siz = s;
}
parent.h:
#ifndef PARENT_H_INCLUDED
#define PARENT_H_INCLUDED
class Parent
{
private:
int price;
public:
Parent();
~Parent();
Parent(int);
Parent(const Parent&);
int getPrice() const;
void setPrice(int);
};
#endif // PARENT_H_INCLUDED
parent.cpp:
#include "Parent.h"
Parent::Parent()
{
}
Parent::Parent(int price)
{
this->price = price;
}
Parent::Parent(const Parent& other)
{
price = other.price;
}
Parent::~Parent () {
}
int Parent::getPrice() const
{
return price;
}
void Parent::setPrice(int p)
{
price = p;
}
非常感谢您的帮助!
I would like to push an instance of Child (Parent is the base class of Child), in a vector using push_back(). But then the value of the Parent members lost.
In the following example, I would like to see 70 when writing the vector to the console, but I get random value instead. What is causing this?
main.cpp:
void demo(vector<Child> &ch) {
Child c(4);
c.setPrice(70);
cout << c.getPrice() << endl; // 70
ch.push_back(c);
cout << ch[ch.size()-1].getPrice() << endl; // random value
}
int main()
{
vector<Child> ch;
demo(ch);
return 0;
}
Child.h:
#ifndef CHILD_H_INCLUDED
#define CHILD_H_INCLUDED
#include "Parent.h"
class Child : public Parent
{
private:
int siz;
public:
Child();
~Child();
Child(int);
Child(const Child&);
int getSiz() const;
void setSiz(int);
};
#endif // CHILD_H_INCLUDED
Child.cpp:
#include "Child.h"
Child::Child()
{
}
Child::Child(int siz)
{
this->siz = siz;
}
Child::Child(const Child& other)
{
siz = other.siz;
}
Child::~Child () {
}
int Child::getSiz() const
{
return siz;
}
void Child::setSiz(int s)
{
siz = s;
}
Parent.h:
#ifndef PARENT_H_INCLUDED
#define PARENT_H_INCLUDED
class Parent
{
private:
int price;
public:
Parent();
~Parent();
Parent(int);
Parent(const Parent&);
int getPrice() const;
void setPrice(int);
};
#endif // PARENT_H_INCLUDED
Parent.cpp:
#include "Parent.h"
Parent::Parent()
{
}
Parent::Parent(int price)
{
this->price = price;
}
Parent::Parent(const Parent& other)
{
price = other.price;
}
Parent::~Parent () {
}
int Parent::getPrice() const
{
return price;
}
void Parent::setPrice(int p)
{
price = p;
}
Thank you very much in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
孩子的复制构造函数仅复制孩子零件。它还应调用父级的构造函数,以使其复制父会员。
The child's copy constructor only copies the child parts. It should also invoke the parent's copy constructor to let it copy the parent members.