Simple pointer class

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Simple pointer class

Post by chronologicaldot »

I'm a lazy programmer. I know we've had several pointer classes shown on this site, particularly some designed specifically for irrlicht. Maybe I just feel uncomfortable with them, but I've never used them. I decided to make yet another pointer class, this time more of a Java style:

Code: Select all

 
// (c) 2014 Nic Anderson (chronologicaldot)
// zlib license
 
namespace util {
 
template<class T>
struct sp
{
    T* p;
 
 
    sp()
    {}
    
    sp( T& item )
        : p( &item )
    {}
    
    sp( T* pointer )
        : p( pointer )
    {}
    
    void set( T& item )
    {
        p = &item;
    }
    
    void set( T* pointer )
    {
        p = pointer;
    }
    
    T& operator = ( T& item )
    {
        p = &item;
        return *p;
    }
    
    T& operator = ( T* pointer )
    {
        p = pointer;
        return *p;
    }
    
    operator T& ()
    {
        return *p;
    }
 
    inline T& o() // object
    {
        return *p;
    }
    
    T& operator * ()
    {
        return *p;
    }
    
    T copy()
    {
        return (T)*p;
    }
};
 
}
 
The usage is naturally short and simple. :)

Code: Select all

 
class TestClass {
public:
  int i;
  TestClass()
    { i = 0; }
  int func()
    { return i; }
  bool like( TestClass other )
    { return i == other.i; }
};
 
sp<TestClass> p1;
TestClass test;
p1 = test;
 
// Direct initialization is allowed
sp<TestClass> p2 = new TestClass();
 
// Object referencing is easy to type
p2.o().i = 1;
 
// casting!!!!
if ( p1.o().like( p2 ) )
{ ... }
 
// The pointer is still accessible!
p1.p = p2.p;
 
 
I have yet to use it extensively, but having worked with Java so long, I'm starting to like the idea of having my code less ambiguous.
What I used to do:

Code: Select all

 
// assume this: TestClass* p = new TestClass();
 
(*p).func();
// or
p->func();
 
The latter still seems quicker to me (at least until you start combining it with casting), but it's not pretty code, and I'll occasionally make the mistake of forgetting the > or - which looks perfectly fine to the compiler in certain cases.

But anyways...
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Simple pointer class

Post by hendu »

You should crank up the warnings instead of such a class, so the compiler will yell if you do a "p-func()" :)
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Re: Simple pointer class

Post by chronologicaldot »

True.
Do you know how to set the warning for that one specifically?

Now that I think about it - what would you do if you were pointing to a standard array?

Code: Select all

 
// let
int* p = new int[2];
// or sp<int> p = new int[2];
//...
int a = (*p)[0]; // The less ambiguous version of *p[0], but God knows someone will use the latter
int b = p-> ?
int c = p.o()[0];
 
Knowing what's going on, I suppose I don't mind any case, though the last one just looks prettier to me. But maybe I've been in Java too long. lol
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Simple pointer class

Post by hendu »

Not sure of the warning, I run with -Wall -Wextra. For your access example, I'd just use direct p[0].
Post Reply