指针 & 类, 输出
Pointers & Classes, Output
提前致谢,
所以,我正在处理包含 指针 的 classes,最后一个是主题
如果我有 class 像下面的实例,我需要一些推送来 显示 [=39= 的指针(堆指针)的数据] 的目标,知道它瞄准(指向)C-tor(成员)中的成员值。
纠正错误的一些帮助。
我添加了一个child class只是为了细节继承,如果你可以添加一些详细信息(如果有的话)。
我得到的值是 7405304 For the Mother Class & 4997952 for the Child Class(对于各自的预期输出不是 9 & 99),如果我可以添加,为什么会这样?
谢谢。
#include <iostream>
class Mother {
public:
Mother(int member) {
mem = new int; /*Heap Ptr*/
mem = &member;
std::cout << "C-tor" << std::endl;
}
~Mother() {
delete mem;
*mem = 0;
std::cout << "D-tor" << std::endl;
}
virtual void display() const {
std::cout
<< *mem
<< std::endl; /*What should i put here ? I've an unexpected value */
}
private:
int* mem = 0;
};
class Child : public Mother {
public:
Child(int a) : Mother(a) {}
~Child() {}
};
void data(Mother const* m) {
m->display();
}
int main() {
int a(9), b(99);
Child child(a);
Mother mother(b);
data(&mother);
data(&child);
return 0;
}
只是对您的代码的几条评论。最重要的是,这一切都与继承有关。不需要将 display
函数声明为虚拟函数,因为这假定您将在 Child
中重新定义它,而您不需要这样做。总之,所有的弊病都来自于指针mem
。在 Mother class 的析构函数中你有这个:
delete mem;
*mem = 0;
这在逻辑上没有意义。在 delete mem
之后指针不存在,因此下一行取消引用新删除的指针。让我们跟随指针的生命周期mem
。它首先在 private
中创建,然后重新分配一个通过 new int
创建的新指针,然后再次重新分配给 &member
(这意味着 delete mem
在析构函数中不会查看 new int
指针,而是查看 &member
)。如果你像这样解决这些问题
class Mother {
public:
Mother(int member) {
*mem = member;
std::cout << "C-tor" << std::endl;
}
~Mother() {
delete mem;
std::cout << "D-tor" << std::endl;
}
void display() const {
std::cout
<< *mem
<< std::endl; /*What should i put here ? I've an unexpected value */
}
private:
int * mem = new int;
};
class Child : public Mother {
public:
Child(int a) : Mother(a) {}
~Child() {}
};
void data(Mother const * m) {
m->display();
}
int main() {
int a(9), b(99);
Child child(a);
Mother mother(b);
data(&mother);
data(&child);
return 0;
}
你应该得到正确的输出。
提前致谢,
所以,我正在处理包含 指针 的 classes,最后一个是主题
如果我有 class 像下面的实例,我需要一些推送来 显示 [=39= 的指针(堆指针)的数据] 的目标,知道它瞄准(指向)C-tor(成员)中的成员值。
纠正错误的一些帮助。
我添加了一个child class只是为了细节继承,如果你可以添加一些详细信息(如果有的话)。
我得到的值是 7405304 For the Mother Class & 4997952 for the Child Class(对于各自的预期输出不是 9 & 99),如果我可以添加,为什么会这样?
谢谢。
#include <iostream>
class Mother {
public:
Mother(int member) {
mem = new int; /*Heap Ptr*/
mem = &member;
std::cout << "C-tor" << std::endl;
}
~Mother() {
delete mem;
*mem = 0;
std::cout << "D-tor" << std::endl;
}
virtual void display() const {
std::cout
<< *mem
<< std::endl; /*What should i put here ? I've an unexpected value */
}
private:
int* mem = 0;
};
class Child : public Mother {
public:
Child(int a) : Mother(a) {}
~Child() {}
};
void data(Mother const* m) {
m->display();
}
int main() {
int a(9), b(99);
Child child(a);
Mother mother(b);
data(&mother);
data(&child);
return 0;
}
只是对您的代码的几条评论。最重要的是,这一切都与继承有关。不需要将 display
函数声明为虚拟函数,因为这假定您将在 Child
中重新定义它,而您不需要这样做。总之,所有的弊病都来自于指针mem
。在 Mother class 的析构函数中你有这个:
delete mem;
*mem = 0;
这在逻辑上没有意义。在 delete mem
之后指针不存在,因此下一行取消引用新删除的指针。让我们跟随指针的生命周期mem
。它首先在 private
中创建,然后重新分配一个通过 new int
创建的新指针,然后再次重新分配给 &member
(这意味着 delete mem
在析构函数中不会查看 new int
指针,而是查看 &member
)。如果你像这样解决这些问题
class Mother {
public:
Mother(int member) {
*mem = member;
std::cout << "C-tor" << std::endl;
}
~Mother() {
delete mem;
std::cout << "D-tor" << std::endl;
}
void display() const {
std::cout
<< *mem
<< std::endl; /*What should i put here ? I've an unexpected value */
}
private:
int * mem = new int;
};
class Child : public Mother {
public:
Child(int a) : Mother(a) {}
~Child() {}
};
void data(Mother const * m) {
m->display();
}
int main() {
int a(9), b(99);
Child child(a);
Mother mother(b);
data(&mother);
data(&child);
return 0;
}
你应该得到正确的输出。