c ++设置空指针? [英] c++ set null pointers?
本文介绍了c ++设置空指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将其设置为null;
how would i set this to null;
LookupTable<Product *> table;
一切我试试说
1 IntelliSense :没有合适的构造函数将int转换为LookupTable
1 IntelliSense: no suitable constructor exists to convert from "int" to "LookupTable"
这里是查找表:
#ifndef LOOKUPTABLE_H
#define LOOKUPTABLE_H
#include <iostream>
#include <string>
using namespace std;
#define MAXRANGE 10
template <class T>
class LookupTable
{
private:
T *aptr[MAXRANGE];
int rangeStart[MAXRANGE];
int rangeEnd[MAXRANGE];
int numRanges;
public:
T defaultValue;
bool failedRangeCheck;
std::string failReason;
// Constructor
LookupTable()
{
numRanges = 0;
defaultValue = T();
}
void addRange(int start, int end)
{
std::cout << "Created a new range... Start: " << start << " / End: " << end << endl;
failedRangeCheck = false;
//lines omitted because not working anyway
if ( !failedRangeCheck )
{
//set ranges
rangeStart[numRanges] = start;
rangeEnd[numRanges] = end;
//build new generic array with end-start+1 positions
//set pointer to point to it
aptr[numRanges] = new T[ end - start + 1 ];
numRanges++;
}
else
{
std::cout << "Range overlapped another range." << endl;
std::cout << failReason << endl;
}
}
T &operator[](int value) // Overloaded [] operator
{
for ( int i = 0; i < numRanges; i++ )
{
if ( (value >= rangeStart[i]) && (value <= rangeEnd[i]) )
{
return aptr[i][value - rangeStart[i]];
}
}
return defaultValue;
}
~LookupTable()
{
delete[] aptr;
numRanges = 0;
}
};
#endif
推荐答案
NULL
- 您将其定义为不同的类型:
Here's how you set it to NULL
- you define it as a different type:
LookupTable<Product *>* table = NULL;
如果你习惯于使用C#,那么你可以把类当作value类型。
If you are used to dealing with C#, then you might be able to think of classes as "value types".
这也可能与您的想法不同。在C ++中:
This might also behave differently than you think. In C++:
LookupTable<Product *> table1;
LookupTable<Product *> table2 = table;
编辑table2时,table1不会改变。
When you edit table2, table1 will not change.
和:
void SomeFunction(LookupTable<Product *> t)
{
// do something with t
}
// ...
LookupTable<Product *> table;
SomeFunction(table);
如果SomeFunction编辑t,则表不会更改。
If SomeFunction edits t, then table doesn't change.
这篇关于c ++设置空指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文