memset,memcpy,strcpy

strcpy

char *strcpy( char *dest, const char *src );

dest-pointer to the byte string to copy to
src-pointer to the null-terminated byte string to copy from

Copies the byte string pointed to by src to byte string pointed to by dest.If the strings overlap, the behavior is undefined.

ps.src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串,返回指向dest的指针。strcpy 就只能拷贝字符串,它遇到’\0’就结束拷贝,不会拷贝’\0’。例如: Continue reading

构造和析构函数浅析

一题简单的构造、析构函数题

#include <iostream>
using namespace std;

struct BaseClass
{
	char ch;
	BaseClass():ch('a') {cout<<ch<<"1";}
	~BaseClass()		{cout<<ch<<"2";}
};

struct MyClass:public BaseClass
{
	MyClass()			{cout<<ch<<"3";}
	~MyClass()			{cout<<ch<<"4";}
};

int main(void)
{
	MyClass x1;
	x1.ch='b';
	MyClass x2;
	x2.ch='c';
	return 0;
}

——出自人人网2011实习生招聘试题

输出结果:a1a3a1a3c4c2b4b2

构造函数(Constructor)由编译器在定义对象时调用,传递到构造函数的第一个参数是this指针,也就是调用这一函数的对象的地址,但是该this指针指向一个没有被初始化的内存块,构造函数的作用就是正确的初始化该内存块。
析构函数(Destructor )在当对象超出它的作用域时由编译器自动调用。析构函数不需要任何参数。 Continue reading