I've been dabbling with C++ for a few months, including templates, smart pointers, and operator overloading. As a neat project for additional education, I thought it'd be fun to create my own smart pointer, aside from (deprecated auto_ptr) and current unique_ptr.
Behold: uber_ptr.
As I've never written a templated class before, and I've never created a smart pointer, nor have I ever tried implementing operating overload, this is fairly intriguing.
My guess is that if you're reading this post here on [H], you're either intrigued as well, amused, or you think I'm an idiot.
Initial goals:
Here's what I have so far:
uber_ptr.h
uber_ptr.cpp
main.cpp
This code builds, but will not link, citing two unresolved external errors:
Any idea on what I'm missing? Feel free to chime in with thoughts, etc.
Behold: uber_ptr.
As I've never written a templated class before, and I've never created a smart pointer, nor have I ever tried implementing operating overload, this is fairly intriguing.
My guess is that if you're reading this post here on [H], you're either intrigued as well, amused, or you think I'm an idiot.
Initial goals:
- Work with any type of pointer (hence, templated)
- Accept a void pointer in the Constructor
- Overload the unary "!" operator as a check for null, so that !<ptr> would return true if null.
Here's what I have so far:
uber_ptr.h
Code:
#ifndef UBER_PTR_H
#define UBER_PTR_H
template<class T> class uber_ptr
{
private:
T * ptr;
public:
// Constructors.
uber_ptr(void);
uber_ptr(void *);
uber_ptr(T);
// Overloaded Operators.
bool operator ! ();
// Destructor.
~uber_ptr(void);
};
#endif
uber_ptr.cpp
Code:
#include "uber_ptr.h"
template<class T> uber_ptr<T>::uber_ptr(void)
{
this->ptr = 0;
}
template<class T> uber_ptr<T>::uber_ptr(void * x)
{
this->ptr = x;
}
template<class T> uber_ptr<T>::uber_ptr(T x)
{
this->ptr = &x;
}
template<class T> bool uber_ptr<T>::operator ! ()
{
if (this->ptr == 0)
return true;
}
template<class T> uber_ptr<T>::~uber_ptr(void)
{
}
main.cpp
Code:
#include <windows.h>
#include "uber_ptr.h"
int main()
{
int a = 1;
int b = 2;
uber_ptr<int> x (a);
return ERROR_SUCCESS;
}
This code builds, but will not link, citing two unresolved external errors:
Error 2 error LNK2019: unresolved external symbol "public: __thiscall uber_ptr<int>::uber_ptr<int>(int)" (??0?$uber_ptr@H@@QAE@H@Z) referenced in function _main C:\Users\jshidell\Desktop\uber_ptr\uber_ptr\main.obj uber_ptr
Error 1 error LNK2019: unresolved external symbol "public: __thiscall uber_ptr<int>::~uber_ptr<int>(void)" (??1?$uber_ptr@H@@QAE@XZ) referenced in function _main C:\Users\jshidell\Desktop\uber_ptr\uber_ptr\main.obj uber_ptr
Any idea on what I'm missing? Feel free to chime in with thoughts, etc.