Some tips about the C++(自己看的,很乱)
来源:互联网 发布:淘宝联通华盛 编辑:程序博客网 时间:2024/04/29 12:01
char str1[] = "abc";
char str2[]="abc";
const char str3[]="abc";
const char str4[]="abc";
char* str5 = "abc";
char* str6="abc";
cout<<(str1==str2)<<endl;//false
cout<<(str3==str4)<<endl;//flase both are arrays in the stack.
cout<<(str5==str6)<<endl;//true , they point to the same area in the constant string area in the memory.
//str5[0] = 'c';//you should not do this, otherwise the program will be abended.
int a[9]=...{0};
cout<<a;//the same as &a ,
cout<<&a;
int* b = new int(9);
cout<<b<<endl;//the address of the new int(9) in the heap area
cout<<&b;//the address of b itself in the stack area
}
指出下面代码的输出,并解释为什么。
main()
{
int a[5]={1,2,3,4,5};
int *ptr=(int *)(&a+1);
printf("%d,%d",*(a+1),*(ptr-1));
}
输出:2,5
*(a+1)就是a[1],*(ptr-1)就是a[4],执行结果是2,5
&a+1不是首地址+1,系统会认为加一个a数组的偏移,是偏移了一个数组的大小(本例是5个int)
int *ptr=(int *)(&a+1);
则ptr实际是&(a[5]),也就是a+5
原因如下:
&a是数组指针,其类型为 int (*)[5];
而指针加1要根据指针类型加上一定的值,
不同类型的指针+1之后增加的大小不同
a是长度为5的int数组指针,所以要加 5*sizeof(int)
所以ptr实际是a[5]
但是prt与(&a+1)类型是不一样的(这点很重要)
所以prt-1只会减去sizeof(int*)
a,&a的地址是一样的,但意思不一样,a是数组首地址,也就是a[0]的地址,&a是对象(数组)首地址,a+1是数组下一元素的地址,即a[1],&a+1是下一个对象的地址,即a[5].
good website about the C+= http://www.w3sky.com/0/277.html
private:
virtual void a()...{cout<<"I am A";};
};
class B:public A
...{
private:
void a()...{cout<<"I am B";};
};
你不能重载或重定义父类的PRIVATE函数。
当程序中throw new Object();
的时候,你只能用catch(object*)来抓获异常,
Object a;
throw a;
你就可以用catch(object)或者catch(object&)来抓获异常。。。
throw "dd";
catch(char*)来抓获
String:
如果要比较两个string,忽视他们的大小写,那么可以使用stricmp(strA.c_str(),strB.c_str());
如果cout一个没有重载<<的操作符的对象,不能编译通过
const string& name;如果在类中如此声明成员变量,那么这个变量只能在类外被修改,类中是不能修改的
static类成员永远也不会在类的构造函数初始化。基类数据成员总是在派生类数据成员之前被初始化,所以使用继承时,要把基类的初始化列在成员初始化列表的最前面。
static 类成员,必须在类声明后,进行 A::a;这样的初始化,如果没有,则编译器报错,默认为0.但非STATIC的成员,不进行初始化,结果是不可预测的。。
c++语言标准关于这个问题的阐述非常清楚:当通过基类的指针去删除派生类的对象,而基类又没有虚析构函数时,结果将是不可确定的。在使用sizeof来衡量一个类的大小的时候,一般不要把static成员变量计入。。。即使你的类是用的纯虚函数,你的对象依然会带有一个虚函数表指针
,值得指出的是,在某些类里声明纯虚析构函数很方便。纯虚函数将产生抽象类——不能实例化的类(即不能创建此类型的对象)。有些时候,你想使一个类成为抽象类,但刚好又没有任何纯虚函数。怎么办?因为抽象类是准备被用做基类的,基类必须要有一个虚析构函数,纯虚函数会产生抽象类,所以方法很简单:在想要成为抽象类的类里声明一个纯虚析构函数。
这里是一个例子:
class awov { // awov = "abstract w/o // virtuals" public: virtual ~awov() = 0; // 声明一个纯虚析构函数 };
这个类有一个纯虚函数,所以它是抽象的,而且它有一个虚析构函数,所以不会产生析构函数问题。但这里还有一件事:必须提供纯虚析构函数的定义:
awov::~awov() {} // 纯虚析构函数的定义
这个定义是必需的,因为虚析构函数工作的方式是:最底层的派生类的析构函数最先被调用,然后各个基类的析构函数被调用。这就是说,即使是抽象类,编译器也要产生对~awov的调用,所以要保证为它提供函数体。如果不这么做,链接器就会检测出来,最后还是得回去把它添上。
int i1, i2, i3;
...
(i1 = i2) = i3; // 合法! i2赋给i1
// 然后i3赋给i1!
允许子父类有一样的成员变量和函数,不过如果要调用的话,最好使用类::标识符。
使用这个办法可以阻止一个类被继承
template <class T>
class inheritance_lock {
friend class T;
inheritance_lock() {}
};
class Usable : private virtual inheritance_lock<Usable> { ... };
class DD:public usable{}
DD dd不会成功,他无法获得最base的类的好、构造函数,因为Usable作为BASE的friend,并不能使得它的子类变成BASE的FRIEND。。
对于没有声明相应参数为const的函数来说,传递一个const对象是非法的。这是一个关于const的很简单的规定。
// 正确的赋值运算符 derived& derived::operator=(const derived& rhs) { if (this == &rhs) return *this;
base::operator=(rhs); // 调用this->base::operator= y = rhs.y;
return *this; } 做子类的赋值函数的时候必须显式的进行BASE类的operator=函数的调用
derived& derived::operator=(const derived& rhs) { if (this == &rhs) return *this;
static_cast<base&>(*this) = rhs; // 对*this的base部分 // 调用operator= y = rhs.y;
return *this; }
这段怪异的代码将*this强制转换为base的引用,然后对其转换结果赋值。这里只是对derived对象的base部分赋值。还要注意的重要一点是,转换的是base对象的引用,而不是base对象本身。如果将*this强制转换为base对象,就要导致调用base的拷贝构造函数,创建出来的新对象(见条款m19)就成为了赋值的目标,而*this保持不变。这不是所想要的结果。
- Some tips about the C++(自己看的,很乱)
- Some Tips about Interbase
- some tips about jsp
- some tips about jdbc
- Some tips about layout
- About GNIT some tips
- Some Tips About C++
- some tips about python
- Python 字典 (很乱,只写给自己看的)
- some tips about shell variables
- Some tips about looping statement
- Some Tips About Layout Resource
- some tips about python One
- some tips about python Two
- some tips about python Three
- some tips about python Four
- some tips about python Six
- Some tips about python Seven
- 黑客眼中安全防范的“四大利器”
- 安全案例:大连商品交易所安全管理平台
- 病毒玩假死
- Web 2.0催生新黑客技术
- 企业级应用防火墙Microsoft ISA Server
- Some tips about the C++(自己看的,很乱)
- Existing lock /var/run/yum.pid: another copy is running. Aborting.错误处理方法
- 什么是动态链接库?
- IDE(Integrated Development,集成开发环境)
- STL
- p2p
- 久违了的Debian
- 视频编辑软件使用攻略
- DDoS防范和全局网络安全网络的应对