本文介绍了类指针值已修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

全部,


我看到一些非常奇怪的东西,并希望有人可能会对我的原因有所了解

看到。我有以下

场景:


平台:Microsoft Windows XP Pro,Visual Studio 2005


代码:


/////////////////////

班级家长

{

int parentData;

};


class Child:public Parent

{

char childData1;

int childData2;

};


void theFunction(Parent * inParent)

{

printf(" inParent =%X \ n",inParent);

}


int _tmain(int argc,_TCHAR * argv [])

{

Child * theChild = new Child;

if( theChild)

{

printf(" theChild =%X \ n",theChild);

theFunction(theChild);

删除theChild;

}


返回0;

}

/////////////////////


我得到以下输出:


theChild = 27373F0

inParent = 27373F4


为什么地址在theFunction中会有所不同?


感谢您的回复。

All,

I''m seeing something very strange and was hoping someone might have
some insight into the cause of what I''m seeing. I have the following
scenario:

Platform: Microsoft Windows XP Pro, Visual Studio 2005

Code:

/////////////////////
class Parent
{
int parentData;
};

class Child : public Parent
{
char childData1;
int childData2;
};

void theFunction(Parent* inParent)
{
printf("inParent = %X\n", inParent);
}

int _tmain(int argc, _TCHAR* argv[])
{
Child* theChild = new Child;
if (theChild)
{
printf("theChild = %X\n", theChild);
theFunction(theChild);
delete theChild;
}

return 0;
}
/////////////////////

I get the following output:

theChild = 27373F0
inParent = 27373F4

Why would the address be different in the theFunction?

Thanks for any replies at all.

推荐答案



我认为''孩子'是一个不好的用词。孩子不是父母,

但你的公共继承意味着。如果您在本例中使用了

这些名称,请使用''Base''和''Derived''下一个

时间。

I think ''Child'' is a bad misnomer. A child is not a parent,
but your public inheritance implies that. If you just used
those names for this example, use ''Base'' and ''Derived'' next
time.



(a)尽量减少使用你的

帖子中的非标准结构。


(b)如果你不使用''argc''或''argv'' ,为什么定义你的主要使用它们?

(a) Try to minimize the use of non-standard constructs in your
posts here.

(b) If you don''t use ''argc'' or ''argv'', why define your main to
use them?



为什么不呢?它们是不同的对象。基本上,在C ++中,
继承派生类对象_contain_基类对象

。基类子对象的内存位置

不一定与包含它的对象相同。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Why not? They are different objects. Essentially, in C++
inheritance derived class objects _contain_ base class objects
in them. The memory location of the base class sub-object is
not necessarily the same as the object that contains it.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




int main()

int main()



因为您正在访问同一对象的不同部分。请注意,

您将指向child的指针传递给一个函数,该指针指向其父级的
。因此,即使地址相同,您仍然可以通过其父指针访问子节点。使用sizeof(...)和

计算孩子的位置以及父母在内存中的位置如果你怀疑这是否为



顺便说一句,我强烈建议使用常量和引用。


void theFunction(const Parent * const p_parent){...}



void theFunction(const Parent& r_parent){...}


注意:

const Parent * p_parent; //是一个可变指针!!

Because you are accessing different parts of the same object. Note that
you are passing a pointer to child to a function taking a pointer to
its parent. So even if the addresses were the same, you would still be
accessing the child through its parent pointer. Use sizeof(...) and
calculate the child''s location and the parent''s location in memory if
you doubt that.

Incidentally, i''ld strongly suggest using constants and references.

void theFunction(const Parent* const p_parent) { ... }
or
void theFunction(const Parent& r_parent) { ... }

Note:
const Parent* p_parent; // is a mutable pointer!!



这篇关于类指针值已修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:42