模板函数调用
Template function calling
本文关键字:函数调用 更新时间:2023-10-16
#include <iostream>
#include <memory>
#include <initializer_list>
#include <cassert>
template <typename T>
class Vector
{
// Your implementation of the Vector class starts here
private:
//attributes
int length;
T* data;
public:
//constructors
Vector()
:length(0),
data(nullptr)
{
std::cout<<"New empty object created"<<std::endl;
}
Vector(int length)
:length(length),
data(new T[length])
{
std::cout<< "Length of object is = " <<length <<std::endl;
}
//use a copy of another Vector
Vector(const Vector& other)
: Vector(other.length)
{
for(int i=0;i<length;i++)
data[i]=other.data[i];
}
// using initializer list
Vector(std::initializer_list<T> list)
: Vector((int)list.size())
{
std::uninitialized_copy(list.begin(), list.end(), data);
std::cout<< "The elements in the object are: ";
for (auto it = std::begin(list); it!=std::end(list); ++it)
std::cout << ' ' << *it;
std::cout<<std::endl;
}
//operators
//copy
Vector<T>& operator=(const Vector<T>& other)
{
if(this!= &other)
{
std::cout<<"Copied constructor"<<std::endl;
delete[] data;
length = other.length;
std::cout<<"New length ="<<length<<std::endl;
data = new T[length];
std::cout<<"The elements in the object are: ";
for(int i=0;i<length;i++)
{
data[i]=other.data[i];
std::cout<<data[i]<<" ";
}
}
std::cout<<std::endl;
//std::cout << "copy operator" << std::endl;
return *this;
}
//move
Vector<T>& operator=(Vector<T>&& other)
{
if(this!= &other)
{
delete[] data;
length = other.length;
data = new T[length];
for(int i=0;i<length;i++){
data[i]=other.data[i];}
other.length = 0;
delete[] other.data;
other.data = nullptr;
}
//std::cout << "Move operator" << std::endl;
return *this;
}
///////////////////////////////////////////////
//add
Vector<T> operator+(const Vector<T>& other) const
{
assert(length == other.length);
Vector<T> newer(length);
std::cout<<"The addition gives: ";
for(auto i=0;i<length;i++)
{
newer.data[i] = data[i]+other.data[i];
std::cout<<newer.data[i]<<" ";
}
std::cout<<std::endl;
return newer;
}
//minus
Vector<T> operator-(const Vector<T>& other) const
{
assert(length == other.length);
Vector<T> newer(length);
for(auto i=0;i<length;i++)
newer.data[i] = data[i]-other.data[i];
return newer;
}
// Multiply by a scalar
Vector<T> operator*(T scalar)
{
Vector<T> newer(length);
std::cout<<"Multiplication of a new vector by scalar provides: ";
for (auto i=0; i<length; i++)
{
newer.data[i] = data[i] * scalar;
std::cout<<newer.data[i]<<" ";
}
std::cout<<std::endl;
return newer;
}
//////////////////////////////////////////
// Add to existing Vector
Vector<T>& operator+=(const Vector<T>& other)
{
for (auto i=0; i<length; i++)
data[i] += other.data[i];
return *this;
}
// Multiply existing Vector by a scalar
Vector<T>& operator*=(T scalar)
{
std::cout<<"Multiplication of an existing vector by scalar provides: ";
for (auto i=0; i<length; i++)
{
data[i] *= scalar;
std::cout<<data[i]<<" ";
}
std::cout<<std::endl;
return *this;
}
double Valueform(int i)
{
return data[i];
}
int Lengthfinder()
{
return length;
}
///////////////////////////////////////////
//destructor
~Vector()
{
delete[] data;
data = nullptr;
length = 0;
}
};
template<typename T>
T dot(const Vector<T>& lhs, const Vector<T>& rhs)
{
// Your implementation of the dot function starts here
T result = 0;
for (auto i=0; i<lhs.Lengthfinder(); i++)
{
result = lhs.Valueform(i)*rhs.Valueform(i);
//result + = multiply(lhs.data[i],rhs.data[i]);
// result += lhs.data[i]*rhs.data[i];
}
return result;
}
//failsafe for value multiplied by a vector
template <typename T, typename I>
Vector<T> operator*(I i,Vector<T> other)
{
std::cout<<"Multiplication was done by a vector and failsafe activated"<<std::endl;
return(other* T(i)) ;
}
int main()
{
Vector<double> a(5);
Vector<double> b({ 1 , 2 , 3 , 4 });
Vector<double> c({ 2 , 3 , 4 });
Vector<double> d({ 5 , 2 , 1 });
// std::cout<< "a=c" <<std::endl;
a = c;
// std::cout<< "e = c+d" <<std::endl;
Vector<double> e;
e = c+d;
// std::cout<< "f = c*5" <<std::endl;
Vector<double> f;
f = c*5;
// std::cout<< "g = 5*d" <<std::endl;
Vector<double> g;
g = 5*d;
Vector<double> Dott;
Dott = dot(c,d);
return 0;
}
代码不允许我调用函数Valueform和Lengthfinder,因为类变量是私有的,所以有人可能解决我可以获得特定数据值和长度的问题吗?这些函数大多是围绕模板工作的,但我只想调用2个函数来获取某些属性,它们给了我不应该存在的错误,尽管我不知道确切的原因。
您的dot
函数采用常量向量:
template<typename T>
T dot(const Vector<T>& lhs, const Vector<T>& rhs)
{ // ^^^^^-----------------^^^^^----- these are const
// Your implementation of the dot function starts here
}
但是,您的成员函数没有标记为const。您应该将它们声明为const以使这些函数可用:
double Valueform(int i) const
{
return data[i];
}
int Lengthfinder() const
{
return length;
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销