有人可以解释一下这段代码 [英] Can some one please explain this code
问题描述
#include< iostream>
A级
{
public:
A():i(1){} int i; };
B级:公共A
{
公开:
B(): j(2){} int j; };
int f(A * p,int count)
{
int total = 0;
for(int i = 0; i< count; ++ i)
{total + = p-> i; }
返回(总计);
}
int main()
{
std :: cout<< f(b,10)<< std :: endl;
std :: cin.get();
返回0;
}
上面代码的输出是15.但是,我预计10?
提前感谢所有人...
# include <iostream>
class A
{
public:
A() : i(1) {} int i; } ;
class B: public A
{
public :
B(): j(2) {} int j ; } ;
int f(A* p, int count)
{
int total = 0 ;
for (int i=0; i < count ; ++i )
{ total += p->i; }
return (total);
}
int main()
{
std::cout << f(b,10) <<std::endl;
std::cin.get() ;
return 0 ;
}
The output of the above code is 15. But, i expected 10?
Thanks in advance to all..
推荐答案
raghukumar写道:
raghukumar wrote:
#include< iostream>
A级
{
public:
A():i(1){} int i; };
B级:公共A
{
公开:
B(): j(2){} int j; };
int f(A * p,int count)
{
int total = 0;
for(int i = 0; i< count; ++ i)
{total + = p-> i; }
返回(总计);
}
int main()
{
std :: cout<< f(b,10)<< std :: endl;
std :: cin.get();
返回0;
}
上面代码的输出是15.
# include <iostream>
class A
{
public:
A() : i(1) {} int i; } ;
class B: public A
{
public :
B(): j(2) {} int j ; } ;
int f(A* p, int count)
{
int total = 0 ;
for (int i=0; i < count ; ++i )
{ total += p->i; }
return (total);
}
int main()
{
std::cout << f(b,10) <<std::endl;
std::cin.get() ;
return 0 ;
}
The output of the above code is 15.
不,代码不能编译,因为:
std :: cout<< f(b,10)<< std :: endl;
^
标识符b没有意义。
>
但是,我预计10?
But, i expected 10?
显而易见的猜测是你没有显示的代码部分没有明确的行为。
Best
Kai-Uwe Bux
The obvious guess would be that the part of the code that you did not show
has undefined behavior.
Best
Kai-Uwe Bux
11月14日上午9:53,raghukumar< raghukumar.r。 .. @ gmail.comwrote:
On Nov 14, 9:53 am, raghukumar <raghukumar.r...@gmail.comwrote:
#include< iostream>
A级
{
public:
A():i(1){} int i; };
B级:公共A
{
公开:
B(): j(2){} int j; };
int f(A * p,int count)
{
int total = 0;
for(int i = 0; i< count; ++ i)
{total + = p-> i; }
返回(总计);
}
int main()
{
std :: cout<< f(b,10)<< std :: endl;
std :: cin.get();
返回0;
}
上面代码的输出是15.但是,我预计10?
先谢谢所有人..
# include <iostream>
class A
{
public:
A() : i(1) {} int i; } ;
class B: public A
{
public :
B(): j(2) {} int j ; } ;
int f(A* p, int count)
{
int total = 0 ;
for (int i=0; i < count ; ++i )
{ total += p->i; }
return (total);
}
int main()
{
std::cout << f(b,10) <<std::endl;
std::cin.get() ;
return 0 ;
}
The output of the above code is 15. But, i expected 10?
Thanks in advance to all..
对不起朋友..
#include< iostream>
A级
{
public:
A():i(1){} int i; };
B级:公共A
{
公开:
B(): j(2){} int j; };
int f(A * p,int count)
{
int total = 0;
for(int i = 0; i< count; ++ i)
{total + = p ++ - > i; }
返回(总计);
}
int main()
{
B b [10];
std :: cout<< f(b,10)<< std :: endl;
std :: cin.get();
返回0;
}
以上是完整的代码。
谢谢,
sorry friends..
# include <iostream>
class A
{
public:
A() : i(1) {} int i; } ;
class B: public A
{
public :
B(): j(2) {} int j ; } ;
int f(A* p, int count)
{
int total = 0 ;
for (int i=0; i < count ; ++i )
{ total += p++->i; }
return (total);
}
int main()
{
B b[10] ;
std::cout << f(b,10) <<std::endl;
std::cin.get() ;
return 0 ;
}
The above is the full code.
thanks ,
>
raghukumar写道:
raghukumar wrote:
>
int f(A * p,int count)
{
int total = 0;
for(int i = 0; i< count; ++ i)
{total + = p ++ - > i; }
>
int f(A* p, int count)
{
int total = 0 ;
for (int i=0; i < count ; ++i )
{ total += p++->i; }
p是指向A的指针,p ++按sizeof(A)递增p。
p is a pointer to A, p++ increments p by sizeof(A).
return(总计);
}
int main()
{
B b [10];
std :: cout<< f(b,10)<< std :: endl;
return (total);
}
int main()
{
B b[10] ;
std::cout << f(b,10) <<std::endl;
您传递的是10 B的数组,B大于A。
-
Ian Collins。
You are passing an array of 10 Bs, a B is bigger than an A.
--
Ian Collins.
这篇关于有人可以解释一下这段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!