[C++] Study Notes: 21 Days to Learn C++ Chapter 1-14 Highlights

Study Notes: 21 Days to Learn C++ Chapters 1-14 Highlights

pointer declaration and deletion

Type* Pointer = new Type;
delete Pointer;

Type* Pointer = new Type[numElements];
delete[] Pointer;

If you don't want to deal with bad_alloc, you can use new(nothrow), which returns NULL when memory allocation fails.

Classes that cannot be copied

class President
{
private:
	President(const President&);
	President& opreator= (const President&);
}

singleton class

Class President
{
private:
	President(){};
	President(const President&);
	const President& operator=(const President&);

	string name;
  
public:
	static President& GetInstance()
	{
	static President onlyInstance;
	return onlyInstance;
	}
}

Use explicit to avoid implicit conversion

Community

union UnionName
{
	Type1 member1;
	Type2 member2;
 
 	...
	TypeN memberN;
};  	

virtual, override guarantees that virtual functions are overridden, final prohibits overriding virtual functions, prohibits inheritance

unary prefix increment operator

Date& operator++()
{
	// operator implementation code
	return *this;
}

postfix increment operator

Data operator++(int)
{
	Data copy(*this);
	// increment implementation code
	return copy; 
}

conversion operator

operator const char*()
{
	ostringstream formattedDate;
	cout << “Holiday is on: ” << holiday << endl;
	dateInString = formattedData.str();
	return dataInString.c_str();
}

smart pointerstd::unique_prt

Move constructor and move assignor

class Sample
{
private:
	Type* ptrResource;
public:
	Sample(Sample&& moveSource)
	{
	ptrResource = moveSource.ptrResource;
	moveSource.ptrResource = NULL;
 	}
	Sample operator=(Sample&& moveSource)
	{
 	if(this !=&moveSource)
	{
     delete[] ptrSource;
     ptrResource = moveSource.ptrResource;
   	moveSource.ptrResource = NULL;
	}
}

Sample();
Sample(const Sample& copySource);
Sample& operator= (const Sample& copySource);
}

user-defined literals

ReturnType operator “” YourLiteral(ValueType value) {// conversion code here}

C++ type conversion operator

static_cast
dynamic_cast // runtime type identification
reinterpret_cast
const_cast

grammar:destination_type result = cast_operator<destination_type>(object_to_cast);

Use #define to define constants#define identifier value

Use macros to avoid being included multiple times

#ifndef HEADER_H_
#define HEADER_H_
...
#endif

Validate expressions using assertassert(expression that evaluates to true or false);

Template function: Calling a template function does not require specifying the template parameter type, but using a template class requires this.

template <typename T1, typename T2 = T1>
bool TemplateFunction(const T1& params1, const T2& params2);

For static members of template classes, the general initialization syntax is
template<typename T> StaticType ClassName<T>::StaticVarName;

Parameter variadic templates (C++14)

template <typename Res, typename First, typename... Rest>
void Sum(Res& result, First val1, Rest... valN)
{
	result = result + val1;
	return Sum(result, valN ...);
}

Use static_assert to perform compile-time checks
static_assert(expression being validated, “Error message when check fails”);

Guess you like

Origin blog.csdn.net/weixin_56917387/article/details/125766875