Function Hiding


If there is a functions in base class and one in subclass that has the same name,
the one in subclass always hides the one in base class, regardless of the parameter list and virtual function or not.
Access base class function through sub class object with a different parameter is a compiling error.
Consider this code snippet:

struct CBase
{
	void funcNonVirtual(int i)
	{
		printf("CBase::funcNonVirtual\n");
	}
};
struct CSub: CBase
{
	void funcNonVirtual(char * string)
	{
		printf("CSub::funcNonVirtual\n");
	}
};
void TestHide()
{
	CSub Sub;
	Sub.funcNonVirtual(2); //comiling error
}
Advertisements

Variadic templates


A function can take any number any type of arguments in in cpp11

template <class T>
void VariadicPrint(T tail)
{
	cout << tail << endl;
}

template <class T, class ...Args>
void VariadicPrint(T head, Args... rest)
{
	cout << head << " ";
	VariadicPrint(rest...);
}

void TestVariadic()
{
	VariadicPrint(1, "a"); //output: 1 a
}

VariadicPrint(1, “a”) outputs “1 a”

decltype in lambda


decltype is to calculate type from object expression
Demonstrated by the lambda used to initialize std::set
decltype is required since that’s the only way to access the type of the lambda when the lambda is embedded.

lambda is no more than syntactic shortcut to functor.

void TestLambdaInSet()
{
	bool bGreat = true;
	auto cmp = [=, &bGreat](int a, int b)
	{
		if (bGreat)
			return a > b;
		else
			return a < b;
	};
	std::set myset(cmp);
	myset.insert(1); myset.insert(2);
	printf("first item should be greatest since bGreat = true. got %d", *myset.begin());
	//output is 2 as expected
	bGreat = false;
	myset.clear();
	myset.insert(1); myset.insert(2);
	printf("first item should be smallest since bGreat = false. got %d", *myset.begin());
	//output is 1 as expected
}

void main()
{
	TestLambdaInSet();
}

functor vs lamba


Demonstrated by std::sort
Comparing to function pointers, lambda can access all variables in enclosing scope.
Comparing to function pointers, functor have state, with help from data members in the functor class.

bool MyCompare(const int& a, const int& b)
{
	return a > b;
}

class MyComparer
{
public:
	MyComparer(bool bCompareByAbsValue)
	{
		mbCompareByAbsValue = bCompareByAbsValue;
	}
	bool operator()(const int& a, const int& b)
	{
		if (mbCompareByAbsValue)
			return abs(a) > abs(b);
		else
			return a > b;
	}
private:
	bool mbCompareByAbsValue;
};
void output(string by, int * aInt, size_t nLen)
{
	cout << "sort by " << by << " descending order: " << endl;
	for (int i = 0; i < nLen; i++)
		cout << aInt[i] << " ";
	cout << endl;

}
void TestFunctionPtrFunctorLambda()
{
	const int LENGTH = 3;
	int nLen = LENGTH;
	int aInt[LENGTH] = {2, -3, 1};
	int * pInt = aInt;
	std::sort(pInt, pInt + nLen, MyCompare);
	output("function pointer", aInt, nLen);

	bool bCompareByAbsValue = true;
	std::sort(pInt, pInt + nLen,
		[bCompareByAbsValue](int a, int b) {
		if (bCompareByAbsValue)
			return abs(a) > abs(b);
		else
			return a > b;
	});
	output("lambda, absolute value", aInt, nLen);

	std::sort(pInt, pInt + nLen, MyComparer(bCompareByAbsValue));
	output("functor, absolute value", aInt, nLen);
}

output:
sort by function pointer descending order:
2 1 -3
sort by lambda, absolute value descending order:
-3 2 1
sort by functor, absolute value descending order:
-3 2 1