类指针值已修改 [英] class pointer value modified

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

问题描述

全部,


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

看到。我有以下

场景:


平台: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.

推荐答案

cw ******* @ cogeco.ca 写道:

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

我看到了。我有以下

场景:


平台:Microsoft Windows XP Pro,Visual Studio 2005


代码:


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

班级家长

{

int parentData;

};


class Child:public Parent
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



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

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

这些名称,请使用''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.


{

char childData1;

int childData2;

};


void theFunction(Parent * inParent)

{

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

}


int _tmain(int argc,_TCHAR * argv [])
{
char childData1;
int childData2;
};

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

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



(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?


{

Child * theChild = new Child;

if(theChild)

{

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

theFunction(theChild);

删除theChild;

}


返回0;

}

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


我得到以下输出:


theChild = 27373F0

inParent = 27373F4


为什么地址在函数中有所不同?
{
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?



为什么不呢?它们是不同的对象。基本上,在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



cw ******* @ cogeco .ca 写道:

全部,


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

场景:


平台: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 [])
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[])



int main()

int main()


{

孩子* theChild =新孩子;

if(theChild)

{

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

theFunction(theChild);

删除theChild;

}


返回0;

}

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


我得到了f ollowing output:


theChild = 27373F0

inParent = 27373F4


为什么地址在theFunction中有所不同?
{
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?



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

您将指向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!!


我认为这是一个Visual Studio问题,因为我用g ++运行代码

3.4.6-3我得到了:


theChild = 804A008

inParent = 804A008

cw ******* @ cogeco.ca 写道:
I think this is a Visual Studio problem because I ran the code in g++
3.4.6-3 and i got:

theChild = 804A008
inParent = 804A008

cw*******@cogeco.ca wrote:

全部,


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

场景:


平台: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 = 2737 3F4


为什么函数中的地址不同?


感谢您的回复。
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆