2道题目,在线等!!

zible 2005-03-17 09:45:19
8. C++
char * pstr = “abc”; sizeof(pstr) = ?

char p[ ]= “abc”; sizeof(p) = ?
void myFunc(char p[])
{
sizeof(p) = ?;
}

struct S1
{
char c;
int i;
};
sizeof(S1) = ?

class X{
public:
X();
virtual ~X();
void myMemberFunc();
static void myStaticFunc();
virtual void myVirtualFunc();
private:
int i;
char * pstr;
char a;
}
sizeof(X) = ?

9. C++
已知:
#include <iostream.h> class Base{public: virtual void f(float x){ cout << "Base::f(float) " << x << endl; }void g(float x){ cout << "Base::g(float) " << x << endl; } void h(float x){ cout << "Base::h(float) " << x << endl; }};
class Derived : public Base{public: virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }void g(int x){ cout << "Derived::g(int) " << x << endl; } void h(float x){ cout << "Derived::h(float) " << x << endl; }};

请写出下列主函数输出给果
void main(void)
{
Derived d;
Base *pb = &d;
Derived *pd = &d;

pb->f(3.14f); // …
pd->f(3.14f); // …

pb->g(3.14f); // …
pd->g(3.14f); // …

pb->h(3.14f); // …
pd->h(3.14f); // …
}
...全文
156 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
popsmart_410108 2005-03-17
  • 打赏
  • 举报
回复
不同的编译环境下空间的分配是不一样的。
比如在VC下 sizeof(int)=4
在TC下 sizeof(int)=2

另外,我更正一下:
最后一个,我在VC下试了下, sizeof(X)=16
但究其原因,还无从得知。
zhousqy 2005-03-17
  • 打赏
  • 举报
回复
4、4、
zible 2005-03-17
  • 打赏
  • 举报
回复
谢谢。向加班着致敬
poplar_1107 2005-03-17
  • 打赏
  • 举报
回复
我的怎么运行着结果是2呢 IBM 600E,跟及其有关系吗? 晕

谁能拯救我的机器
popsmart_410108 2005-03-17
  • 打赏
  • 举报
回复
8题:
在32位的机器上答案是
4 char *p 指针类型为4个字节
4 这里是字符串长度为4 “abc/0”
4 char p[]在函数的形参传递过程中变成了指针也就跟头一个一样了
8 4+4
12 4+4+4
yuchengliu 2005-03-17
  • 打赏
  • 举报
回复
这个是运行结果
4
myfunc 4
8
yuchengliu 2005-03-17
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

struct S1
{
char c;
int i;
};

void myFunc(char p[])
{
cout << "myfunc " << sizeof(p) <<endl;//这个是指针的

}
int main()
{
char * pstr = "abc";
cout << sizeof(pstr) <<endl; //这个是指针的 ---4
char p[ ]= "abcd";
//cout << sizeof(p) <<endl;
myFunc(p);//这个是指针的 ---4

cout << sizeof(S1) <<endl;//这个是结构体的 ,按内存排列--8
system("pause");

}
poplar_1107 2005-03-17
  • 打赏
  • 举报
回复
#include<iostream.h>
int main(){
char *p="ABC";
int a;
a=sizeof(p) ;
cout<<a;
return 0;
}
结果为2 嘎嘎~~
kobefly 2005-03-17
  • 打赏
  • 举报
回复
Derived::f(float) 3.14
Derived::f(float) 3.14
Base::g(float) 3.14
Derived::g(int) 3
Base::h(float) 3.14
Derived::h(float) 3.14
xue23 2005-03-17
  • 打赏
  • 举报
回复
sizeof(p) = 4
sizeof(p) = 4 相同的,都是可以看成数组。
sizeof(S1) = 8;
sizeof(X)= 4 * 4 = 16

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧