typename
typename
Before 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 typename
occur
here typename
is to point out SubType
is class T
a type.
Of course, typename
it can also be used instead of keywords class
.
template <typename T> class Myclass;
Member Template
Members 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);
d
And i
are the double
type and int
type, 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 = init
can 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 != X
the time, when that is not the same as two types, we Myclass
are assign()
the parameters x
and *this
do not the same type, then we will not be able to assign()
directly access in Myclass
the private与protected
members, 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 Myclass
the public
variable 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
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
命名空间
namespace
Different set of identifiers in the scope of a named. If the namespace
definition of all identifiers within, then namespace
this will be the only sound name may conflict with other global symbols of identifiers. Then add the identifier before namespace
it can be applied namespace
symbols, with among ::
separated.
And namespace std
it contains all the identifiers defined therein.
In addition, using
use 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;
}
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 explict
is prohibited automatic conversion constructor is called.
Notice the difference between what the two operations
X x;
Y y(x);//显式调用
X x;
Y y = x;//隐式调用