Please see the following code:
// templateClassTemplate.cpp
#include <iostream>
class Account{
public:
explicit Account(double amount=0.0): balance(amount){}
void deposit(double amount){
balance+= amount;
}
void withdraw(double amount){
balance-= amount;
}
double getBalance() const{
return balance;
}
private:
double balance;
};
template <typename T, int N>
class Array{
public:
Array()= default;
int getSize() const;
private:
T elem[N];
};
template <typename T, int N>
int Array<T,N>::getSize() const {
return N;
}
int main(){
std::cout << std::endl;
Array<double,10> doubleArray;
std::cout << "doubleArray.getSize(): " << doubleArray.getSize() << std::endl;
Array<Account,1000> accountArray;
std::cout << "accountArray.getSize(): " << accountArray.getSize() << std::endl;
std::cout << std::endl;
}
This code is taken from a learning course on template initialisation.
I have two questions:
-
How is the object
Array<double,10> doubleArray>
initialised? There is a default constructor that takes no arguments. How is the object intialised? -
How is the object
Array<Account,1000> accountArray
initialised? I can imagine that the first template parameter instantiates an Account object, but how is it constructed?
Thanks!
Aucun commentaire:
Enregistrer un commentaire