gpt4 book ai didi

c++ - 派生函数图

转载 作者:太空狗 更新时间:2023-10-29 21:15:44 26 4
gpt4 key购买 nike

我有基类和派生类基础:

class Person{
public:
Person(string name , int age ){
this -> name = name;
this -> age = age;
}
virtual void getInfo(){
cout << "Person " << name << " " << age;
}

protected:
string name;
int age;
};

派生:

class Kid : public Person{
public:
Kid(string name, int age):Person(name,age){};
virtual void getInfo( ){
cout << "Kid " << name << " " << age;
}
};
class Adult : public Person{
public:
Adult(string name, int age):Person(name,age){};
virtual void getInfo( ){
cout << "Adult " << name << " " << age;
}
};

当我做类似的事情时

map<string ,Person*> m;
Person *one;
Person *three;
Kid two("Mathew",15);
Adult four("JOhn",55);
three = &four;
one = &two;
m["first"] = one;
m["second"] = three;
for( auto &x : m )
x.second-> getInfo();
return 0;

它很好地打印了它应该打印的信息//“Adult”代表成人类(class),“Kid”代表 child 类(class)

但是,当我编辑类并将 map 移动到基类时。例如,创建 Add 方法。

 class Person{
public:
Person(string name , int age ){
this -> name = name;
this -> age = age;
}
virtual void getInfo(){
cout << "Person " << name << " " << age;
}
void add( string name , Person a){
Person *one = &a;
m[ name ] = one;
}
void print(){
for( auto &x: m )
x.second -> getInfo()
}
protected:
string name;
int age;
map< string , Person*> m;
};



Person one("John", 25);
one.add("first",Kid("Mathew",15));
one.add("second",Adult("Leo",55));
one.print();

它抛出 seg fault ,为什么会这样?它的实现与 using of 方法基本相同。是什么导致段错误?有办法解决吗?

//编辑

我尝试使用 unique_ptr 重新定义 map 作为

map< string ,  unique_ptr<Person>> m;
AddField (string name , Person a ){

m[name] = ( unique_ptr<Person> (a));
return *this;
}

  properties[name] = unique_ptr<Person> ( new Person( a ));

 AddField (string name , Person   a ){

CData *one = unique_ptr<Person>(new Person(a));
m[name] = one ;
return *this;
}

我没有使用唯一/共享 ptr 的经验。这扔了

‘std::unique_ptr’ to ‘Person*’

最佳答案

首先,让我们了解 map 实际存储的内容:

map< string , Person*> m;

这个映射将一个字符串绑定(bind)到一个Person*,这是一个指向一个人的内存地址。该映射实际上并不存储 person 实例,仅存储其内存地址。

现在让我们分析一下你的两种情况。

 map<string ,Person*> m;

// Allocating space for two memory addresses on the stack:
Person *one;
Person *three;

// Creating two instances on the stack:
Kid two("Mathew",15);
Adult four("JOhn",55);

// Setting the pointers to the memory addresses of the instances on the stack:
three = &four;
one = &two;

m["first"] = one;
m["second"] = three;
for( auto &x : m )
x.second-> getInfo();
return 0;

// End of the function: now all objects are destroyed in reverse order.

实例 twofour 存在于堆栈中,并在作用域结束时被销毁(在 return 0; 之后)。获取它们的内存地址并将它们存储到 onethree 中就可以了,因为指针的生命周期将超过 twofour .


Person one("John", 25);

// Creating a Kid instance without a name (temporary).
// The Kid instance goes out of scope immediately and is destroyed:
one.add("first",Kid("Mathew",15));

// Creating an Adult instance without a name (temporary).
// The Adult instance goes out of scope immediately and is destroyed:
one.add("second",Adult("Leo",55));

one.print();

这里的问题是您正在创建的实例被销毁得太快了。您需要正确管理它们的生命周期,以确保您插入到 map 中的内存地址不会比指向的内存位置中的数据更长寿。

另一个主要问题是您按值接受addPerson a 参数。这将创建传递实例的拷贝。

// Copy the passed instance:
void add( string name , Person a){
// Take memory address of the copied instance:
Person *one = &a;
m[ name ] = one;

// The copied instance is destroyed here!
}

您应该将a 作为引用,以避免复制和object slicing :

void add( string name , Person& a){
m[ name ] = &a;
}

这是一个工作示例,在更正 a 的签名之后:

Kid k("Mathew",15);
Adult a("Leo",55);

// k and a will outlive one.

Person one("John", 25);
one.add("first", k);
one.add("second", a);

one.print();

// one gets destroyed.
// a gets destroyed.
// k gets destroyed.

关于c++ - 派生函数图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36955281/

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