有这个几个问题有点糊涂,请教下:
有这个几个问题有点糊涂,请教下:
1.头文件中的ifndef/define/endif是干啥用?
书上答案:防止该头文件被重复使用。
我想:
现在有A.cpp,B.cpp和my.h,A.cpp和B.cpp都有#include "my.h",
那么,A.cpp使用了#include "my.h"后,
ifndef/define肯定做了,那么等B.cpp中使用#include "my.h"的时候,
不是无法包含my.h中的内容了吗?
比如my.h中这样写:
ifndef MY_H
#define MY_H
...//内容
...
#endif
2.
#include <iostream>
#include <string>
using namespace std;
void main()
{
string strArr1[] = {"Thrend","Micro","soft"};
for(int i=0;i<sizeof(strArr1)/sizeof(string);i++)
cout<<strArr1[i];
}
请问:为何sizeof(strArr1)=12( 书上答案) ?难道
3.
#include <iostream>
#include <string>
using namespace std;
void main()
{
string strArr1[] = {"Thrend","Micro","soft"};
for(int i=0;i<sizeof(strArr1)/sizeof(string);i++)
cout<<strArr1[i];
cout<<" ";
printf("%d,%d\n",sizeof(strArr1),sizeof(string));
}
为何sizeof(strArr1)=48?
4.
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
Base (){cout<<"Base-ctor"<<endl;}
~Base(){cout<<"Base-dtor"<<endl;}
//virtual void f(){cout<<endl;} //语句3
};
void main()
{
Base base;
printf("%d\n",sizeof(Base));
}
为何语句3注释掉,输出是1,而没注释输出是4?
5.
int *ptr;
ptr=(int*)0x8000;
*ptr=oxaabb;
问:这样有啥问题?
书上答案:这样会导致运行时候错误,因为这样给指针一共随意地址,非常危险。
我想:这样不是把地址0x8000给ptr了吗?没有随便给地址啊。0x8000就是一个确定地址啊。
6.请问,c++中有了malloc/free,为何还需要new/delete?
7.
class Empty
{
public:
};
c++中默认产生哪些类成员函数(一共4个)
8.
char *m_data=new char[5];
delete [] m_data;
delete m_data;
问:哪个delete操作正确?如果有错,错在哪里?
9.一共函数定义:
void f(const char *str)
{
.....
}
这里const作用?
10.
#include <iostream>
#include <string>
using namespace std;
int main()
{
printf("%d",5.01);
}
为何输出一个大数?