开发者

c++ set null pointers?

how would i set this to null;

LookupTable<Product *> table;

everything i try says

1 IntelliSense: no suitable constructor exists to convert from "int" to "LookupTable"

Here is what Lookup table is:

#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


In your example, table isn't a pointer. It is an object of type LookupTable, where LookupTable is a template class. In this case, you happen to be making a LookupTable specialized for Product *s, but you would get the same error if you did LookupTable< int > table = 0;


Here's how you set it to NULL - you define it as a different type:

LookupTable<Product *>* table = NULL;

If you are used to dealing with C#, then you might be able to think of classes as "value types".

This might also behave differently than you think. In C++:

LookupTable<Product *> table1;
LookupTable<Product *> table2 = table;

When you edit table2, table1 will not change.

And:

void SomeFunction(LookupTable<Product *> t)
{
    // do something with t
}

// ...

LookupTable<Product *> table;
SomeFunction(table);

If SomeFunction edits t, then table doesn't change.


You're declaring table as a type of LookUpTable with the meta type of pointer to a Product.


That's a table of pointers, not a pointer to a table (of pointers). Did you mean LookupTable<Product *> *table; ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜