Which Operators to Overload?

closed account (zb0S216C)
When creating a class that allocates resources, which operators should be overloaded? Consider this code segment, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ResourceAllocator
{
    public:
        ResourceAllocator( void );
       ~ResourceAllocator( void );
 
    protected:
        // _pAllocator: Used to allocate the resources for an undefined
        // reason. This is an example code segment after all.
        int *_pAllocator;
    
    public:
        int operator [ ] ( const unsigned int iIndex );

        // Return a boolean value to clearly indicate a successful/unsuccessful       
        // operation assignment.
        int &operator = ( const ResourceAllocator &rAllocator );
};

In the above code segment, would it be beneficial to overload the sub-script, and equals assignment operators rather than use methods? Also, what other operator( s ) should be overloaded in a class such as this( if any )?

Notice: This code has been modified.

Wazzak
Last edited on
It is customary to for operator= to return a reference to the object, and not bool.

If you are not storing something that requires indexed access, I don't see the need for operator[].

For an allocator, I would think it has an Allocate() method and a Free() method. I don't see myself mapping those operations to operators.
closed account (zb0S216C)
Thank you for your input, WebJose. I've modified the code.

webJose wrote:
For an allocator, I would think it has an Allocate() method and a Free() method. I don't see myself mapping those operations to operators.

It does seem cleaner to use methods for allocation and deletion. For now, I think I'll stick with the basic operators such as sub-script, and equals assignment.

Wazzak
Topic archived. No new replies allowed.