C++ 派生类构造函数中此指针的函数 运算符[](计数)=对象[计数]; } //******************************************************* //findItem函数* //此函数用于搜索项目。如果找到项目* //返回下标。否则返回-1* //******************************************************* 模板 int SearchableVector::findItem(常量项) { 对于(int count=0;count size();count++) { if(this->getElementAt(count)==项目) 返回计数; } 返回-1; } #恩迪夫
这是派生类SearchableVector的代码:C++ 派生类构造函数中此指针的函数 运算符[](计数)=对象[计数]; } //******************************************************* //findItem函数* //此函数用于搜索项目。如果找到项目* //返回下标。否则返回-1* //******************************************************* 模板 int SearchableVector::findItem(常量项) { 对于(int count=0;count size();count++) { if(this->getElementAt(count)==项目) 返回计数; } 返回-1; } #恩迪夫,c++,C++,这是派生类SearchableVector的代码: #ifndef SEARCHABLEVECTOR_H #define SEARCHABLEVECTOR_H #include "SimpleVector.h" template <class T> class SearchableVector : public SimpleVector<T> { public: // Default constructor SearchableVector() : Simp
#ifndef SEARCHABLEVECTOR_H
#define SEARCHABLEVECTOR_H
#include "SimpleVector.h"
template <class T>
class SearchableVector : public SimpleVector<T>
{
public:
// Default constructor
SearchableVector() : SimpleVector<T>()
{}
// Constructor
SearchableVector(int size) : SimpleVector<T>(size)
{ }
// Copy constructor
SearchableVector(SearchableVector &);
// Accessor to find an item
int findItem(const T);
};
//*******************************************************
// Copy constructor *
//*******************************************************
template <class T>
SearchableVector<T>::SearchableVector(SearchableVector &obj):
SimpleVector<T>(obj.size())
{
std::cout << this->size() << std::endl;
for(int count = 0; count < this->size(); count++)
this->operator[](count) = obj[count];
}
//*******************************************************
// findItem function *
// This function searches for item. If item is found *
// the subscript is returned. Otherwise -1 is returned. *
//*******************************************************
template <class T>
int SearchableVector<T>::findItem(const T item)
{
for (int count = 0; count <= this->size(); count++)
{
if (this->getElementAt(count) == item)
return count;
}
return -1;
}
#endif
\ifndef searchable vector\u H
#定义可搜索向量
#包括“SimpleVector.h”
模板
类SearchableVector:公共SimpleVector
{
公众:
//默认构造函数
SearchableVector():SimpleVector()
{}
//建造师
SearchableVector(整型大小):SimpleVector(大小)
{ }
//复制构造函数
SearchableVector(SearchableVector&);
//用于查找项目的访问者
int findItem(const T);
};
//*******************************************************
//复制构造函数*
//*******************************************************
模板
SearchableVector::SearchableVector(SearchableVector和obj):
SimpleVector(对象大小())
{
std::cout size()size();count++)
此->运算符[](计数)=对象[计数];
}
//*******************************************************
//findItem函数*
//此函数用于搜索项目。如果找到项目*
//返回下标。否则返回-1*
//*******************************************************
模板
int SearchableVector::findItem(常量项)
{
对于(int count=0;count size();count++)
{
if(this->getElementAt(count)==项目)
返回计数;
}
返回-1;
}
#恩迪夫
从昨天起,我已经被困了好几个小时,但我仍在努力想办法。我希望你们能帮我解决这些问题。在构造函数中,
这个总是指向正在构造的对象。(对于其他成员函数,它指向调用成员函数的对象。)
this->
的所有用法都可以删除,因为此处不需要它们。如果成员函数中存在与类成员同名的参数或局部变量,则可能需要使用this->
来引用类成员(尽管重命名变量以避免混淆通常更可取)
删除此->
将保留操作员[](计数)
,这是合法的,但可以重写为(*此)[count]
(如果需要使用此
)
由于template SearchableVector::SearchableVector(SearchableVector&obj)
定义了一个复制构造函数,因此将首先复制基类SimpleVector
。然后,当调用size()
时,它将返回复制的大小(即被复制向量的大小)。请注意,复制构造函数的参数通常是常量&
,因为您不应该修改传入的值。谢谢您提供的信息。那么,在这种情况下,这个
指向的是SearchableVector类的对象?但实际上,当我把光标移到这个上时,它说它指向SimpleVector,我不太明白它的意思。@Victor10214当你“把光标移到这个上”时,你使用的是什么函数?这是在调试器中还是在IDE中?在SimpleVector
构造函数中,此
指向一个SimpleVector
(因为虽然已为派生类分配了内存空间,但尚未构造它),在SearchableVector
构造函数中,相同的指针现在指向一个SearchableVector
对象。好的,基本上在SearchableVector::SearchableVector(SearchableVector&obj):SimpleVector(obj.size())
中,这个首先指向一个SimpleVector对象。然后,由于现在派生类构造函数正在运行,因此相同的this
现在指向一个SearchableVector。我想知道我是否理解正确?
#ifndef SEARCHABLEVECTOR_H
#define SEARCHABLEVECTOR_H
#include "SimpleVector.h"
template <class T>
class SearchableVector : public SimpleVector<T>
{
public:
// Default constructor
SearchableVector() : SimpleVector<T>()
{}
// Constructor
SearchableVector(int size) : SimpleVector<T>(size)
{ }
// Copy constructor
SearchableVector(SearchableVector &);
// Accessor to find an item
int findItem(const T);
};
//*******************************************************
// Copy constructor *
//*******************************************************
template <class T>
SearchableVector<T>::SearchableVector(SearchableVector &obj):
SimpleVector<T>(obj.size())
{
std::cout << this->size() << std::endl;
for(int count = 0; count < this->size(); count++)
this->operator[](count) = obj[count];
}
//*******************************************************
// findItem function *
// This function searches for item. If item is found *
// the subscript is returned. Otherwise -1 is returned. *
//*******************************************************
template <class T>
int SearchableVector<T>::findItem(const T item)
{
for (int count = 0; count <= this->size(); count++)
{
if (this->getElementAt(count) == item)
return count;
}
return -1;
}
#endif