c ++ new features _1

typename
typenameBefore other suitable type of identifier, such as:

#include <iostream>  
#include <vector>  
using namespace std;  
class Num{
public:
	vector<int> num;
	Num(){num.push_back(1),num.push_back(2);}
	typedef int SubType;
};
template<class T>  
class Myclass{
private:
	int sum;
public:
	Myclass(int _sum = 0):sum(_sum){ }
	typename T::SubType	Sum( T coll){
		for(int i = 0;i <= 1;i++)
			sum += coll.num[i];
		return sum;
	}
};
int main()
{
	Num aa;
	Myclass<Num> a(0);
	cout << a.Sum(aa) << endl;
	return 0; 
}

If not typenameoccur
Here Insert Picture Description
here typenameis to point out SubTypeis class Ta type.
Of course, typenameit can also be used instead of keywords class.

template <typename T> class Myclass;

Member TemplateMembers of the template, the template is then added to a template declaration Yuding Yi, such as

#include<iostream>
using namespace std;

template <class T>
class Myclass {
private:
	T value;
public:
	Myclass(T _a) {
		value = _a;
	}
	template<class X>
	void assign(const Myclass<X>& x) {
		cout << x.getValue() << endl;
	}
	T getValue()const {
		return value;
	}
};
int main() {
	Myclass<double> d(1.2);
	Myclass<int> i(11);
	d.assign(d);
	d.assign(i);
	return 0;
}

one of them

	Myclass<double> d(1.2);
	Myclass<int> i(11);
	d.assign(d);
	d.assign(i);

dAnd iare the doubletype and inttype, and then call d.assign(d)when the incoming season T = double,X = double, it d.assign(d)will be able to call normal inline template function template<class X> void assign(const Myclass<X>& x){ }; in addition d.assign(i);order T = double,X = initcan also be normal calling function. This is the advantage of inline template can be automatically converted between the two types, using different inline template functions.
But it should be noted that, when T != Xthe time, when that is not the same as two types, we Myclassare assign()the parameters xand *thisdo not the same type, then we will not be able to assign()directly access in Myclassthe private与protectedmembers, then we would only additionally set a function for accessing the value of the above code implementation

T getValue()const {
		return value;
	}

Well, actually this is because assign()of the different types of variables can still access Myclassthe publicvariable or function.


基本型别的显式初始化
If no parameters, explicit constructor call syntax, basic type is initialized 0, such as:

#include<iostream>
using namespace std;
int main() {
	int i = 3;
	i = int();
	cout << i << endl;
	return 0;
}

The results of
Here Insert Picture Description
course, this function more suitable for initialization value in the template function, to ensure that any type do not have a precise initial value, such as
Here Insert Picture Description


命名空间
namespaceDifferent set of identifiers in the scope of a named. If the namespacedefinition of all identifiers within, then namespacethis will be the only sound name may conflict with other global symbols of identifiers. Then add the identifier before namespaceit can be applied namespacesymbols, with among ::separated.

And namespace stdit contains all the identifiers defined therein.

In addition, usinguse is also common: a statement using only introduced once a member of the namespace, such as:

#include<iostream>
using std::cout;
int main()
{
    int x;
   // cin >> x;//wrong
    std::cin >> x;//right
    cout << x;//right
    return 0;
}

Shorthand name can only declare its scope and its use nested scopes, once the scope is over, you must use the fully qualified name, such as:

#include<iostream>
using namespace std;
namespace std{
    void print(int i) {
        cout << i << endl;
    }
}
void print(float f) {
    cout << f << endl;
}
int main()
{
    using std::print;//嵌套使用
    print(2.2);
    print(1);
    return 0;
}

Here Insert Picture Description


explict
For inhibiting 单参数构造函数for automatic type conversions, such as

class Myclass{
	explict Myclass(int num){ ... }

At this point we would not be in such a call, such as
Myclass a = 12;because we use explictis prohibited automatic conversion constructor is called.
Notice the difference between what the two operations

X x;
Y y(x);//显式调用
X x;
Y y = x;//隐式调用
Published 222 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44116998/article/details/104706341