这个程序如何用C ++提供输出请解释一下 [英] How this program is giving output in C++ please explain

查看:66
本文介绍了这个程序如何用C ++提供输出请解释一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream.h>
#include<conio.h>

class base
{
public:
    int bval;
    base()
    {
        bval=0;
    }
};

class deri:public base
{
public:
    int dval;
    deri()
    {
        dval=1;
    }
};

void SomeFunc(base *arr,int size)
{
    for(iont i=0;i<size;i++,arr++)
        cout<<arr->bval;
    cout<<endl;
}

int main()
{
    base BaseArr[5];
    SomeFunc(BaseArr,5);
    deri DeriArr[5];
    SomeFunc(DeriArr,5);
}





我的尝试:



这个程序输出为:

00000

01010



What I have tried:

this program is giving output as :
00000
01010

推荐答案

这是因为你的 SomeFunc()函数使用指向 base 类的指针。



在循环中,传递参数的已使用地址递增 sizeof(base)。但是对于 deri 类,它应该增加 sizeof(派生)以获得指向下一个项目的有效指针。因为base的大小为4字节/ sizeof(int) derived 大小的两倍,指针将用完同步。



deri 的内存布局是:

That is because your SomeFunc() function uses a pointer to the base class.

Within the loop the used address of the passed argument is incremented by sizeof(base). But for the deri class it should be incremented by sizeof(deri) to get a valid pointer to the next item. Because base has a size of 4 bytes / sizeof(int) and deri twice the size, the pointer will run out of sync.

The memory layout of deri is:
int bval;
int dval;

有效访问的值是:

The values effectively accessed are then:

Iteration  base      deri
0          bval[0]   bval[0] = 0
1          bval[1]   dval[0] = 1
2          bval[2]   bval[1] = 0
3          bval[3]   dval[1] = 1
4          bval[4]   bval[2] = 0


简单:那个代码无法编译。所以你正在运行的是来自不同代码集的输出。 oint 对于初学者来说不是一个已知类型。

但如果确实如此,那就不会做你认为应该做的事了,原因是这个:

Simple: that code doesn't compile. So what you are running is the output from a different set of code. oint is not a known type for starters.
But if it did, it wouldn't do what you think it should, and the reason is this:
void SomeFunc(base *arr,int size)
{
    for(iont i=0;i<size;i++,arr++)
        cout<<arr->bval;




deri DeriArr[5];
SomeFunc(DeriArr,5);

当你将DeriArr传递给SomeFunc时,它作为Base *到达 - 这是完全有效的,因为数组的名称是指向数组中第一个元素的指针,而Deri是派生自Base,所以指向Deri的指针是指向Base的有效指针。

然后你增加指针: arr ++ 作为for循环。

就SomeFunc而言,arr是指向Base的指针,因此++将Base的大小(以字节为单位)添加到指针中。但是Deri比Base大 - 它有一个额外的整数。因此,递增指针并不指向下一个Deri元素,它指向下一个Base元素,并在Deri实例的一半处结束。并且:你得到你看到的结果。

When you pass DeriArr to SomeFunc, it arrives as a Base* - which is perfectly valid, as the name of an array is a pointer to the first element in the array, and Deri is derived from Base, so a pointer to a Deri is a valid pointer to a Base.
But then you increment the pointer: arr++ as part of the for loop.
As far as SomeFunc is concerned, arr is a pointer to a Base, so ++ adds the size of a Base in bytes to the pointer. But Deri is bigger than Base - it has an extra integer. So incrementing the pointer doesn't point to the next Deri element, it points to the next Base element and ends up half way through the Deri instance. And lo: you get the result you see.


Quote:

这个程序如何提供输出C ++请解释

How this program is giving output in C++ please explain



自己看看调试器。

有一个工具可以让你看到你的代码在做什么,它的名字是调试器的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


See by yourself with debugger.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于这个程序如何用C ++提供输出请解释一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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