1

I'm working on an cross-platform environment for wrapping other libraries, especially because that is helping me to understand C++ and its libraries/extensions, since i'm quite new to the programming language.

I'm creating wrappers for primitive types, so i can give them functions like, eventSerialize(...), eventRepresentate(...), etc. (And because some other reasons).

To save time, I thought of making a template class wrapper for primitives.

I have the following questions:

  • Am I doing the references of the operators properly?
  • How to make sure they can be casted to each other? (template operator Primitive();)?
  • What about all the other operators, I mean, I can find them, but where I can find how I should write them properly with the right syntax (like const / &)?

I hope someone can give me those answers.

#ifndef swift_Int32
#define swift_Int32
#include <boost/cstdint.hpp>

namespace swift
{
template<class T> class Primitive : public Object
{
    T data;
public:
    Primitive();
    Primitive(T&);
    Primitive(Primitive<T>&);

    Primitive<T>& operator = (const Primitive<T>&);

    Primitive<T> operator * (const Primitive<T>& value);
    Primitive<T> operator / (const Primitive<T>& value);
    Primitive<T> operator + (const Primitive<T>& value);
    Primitive<T> operator - (const Primitive<T>& value);
    Primitive<T>& operator *= (const Primitive<T>& value);
    Primitive<T>& operator /= (const Primitive<T>& value);
    Primitive<T>& operator += (const Primitive<T>& value);
    Primitive<T>& operator -= (const Primitive<T>& value);

    bool operator == (const Primitive<T>&) const;
    bool operator != (const Primitive<T>&) const;
    bool operator <  (const Primitive<T>&) const;
    bool operator >  (const Primitive<T>&) const;
    bool operator <= (const Primitive<T>&) const;
    bool operator >= (const Primitive<T>&) const;
};


typedef Primitive<bool> Bool;
typedef Bool Bit;
typedef Primitive<int8_t> Char;
typedef Primitive<uint8_t> Byte;
typedef Primitive<int16_t> Int16;
typedef Primitive<uint16_t> UInt16;
typedef Primitive<int32_t> Int32;
typedef Primitive<uint32_t> UInt32;
typedef Int16 Short;
typedef UInt16 UShort;
typedef Int32 Int;
typedef UInt32 UInt; 
} 

#endif //swift_Int32
Tim
  • 5,521
  • 8
  • 36
  • 69
  • You forgot about bitwise and logical operators. – cdhowie Oct 03 '12 at 15:37
  • See question number 3. But thanks for your comment. – Tim Oct 03 '12 at 15:39
  • The general rule for operators is: if you could use the result as an lvalue normally, return a reference. Otherwise, return a copy. – cdhowie Oct 03 '12 at 15:42
  • It is always a good idea to have the non-modifying binary operators as free functions, because otherwise only the second argument will allow implicit conversions. – celtschk Oct 03 '12 at 15:45
  • A [related question](http://stackoverflow.com/q/8058813/900626) on SO has some useful info in the answers. – Mark Taylor Oct 03 '12 at 15:52

0 Answers0