views:

65

answers:

3

What I'm trying to do is create a new custom data type that behaves like all other primitive types. Specifically, this data type appears like a Fixed Point fraction. I've created a class to represent this data type, called "class FixedPoint", and in it there are ways to typecast from "FixedPoint" to "int" or "double" or "unsigned int", etc. That is fine.

Now, what if I want to cast from "int" to "FixedPoint"? Originally my solution was to have a constructor:

FixedPoint(int i) { /* some conversion from 'int' to 'FixedPoint' in here */ }

This works...but you cannot put it into a union like so:

 union {
   FixedPoint p;
 };

This will break, because "FixedPoint" does not have an implicit trivial constructor (we just defined a constructor, "FixedPoint(int i)").

To summarize, the whole issue is "we want to cast from some type T to type FixedPoint without explicitly defining a constructor so we can use our type FixedPoint in a union".

What I think the solution is but cannot find any evidence online: Define an overloaded global typecast operator to cast from "int" to "FixedPoint".

Is there a way to do this without using class constructors? I'd like to be able to use this class in a union. What I've tried (in global scope):

operator (FixedPoint f, int a) { ... } //compiler complains about this is not a method or non-static.

And a little example to show unions don't like constructors (they like POD)

class bob
{
  public:
    bob(int a) { m_num = a; }
  private:
    int m_num;
};
void duck()
{
  union 
  {
    bob a;
  };
}

This error seen in Visual Studio is:

error C2620: member 'duck::<unnamed-tag>::a' of union 'duck::<unnamed-tag>' has user-defined constructor or non-trivial default constructor

Any ideas? Thanks

A: 

Custom conversion operators must be a member of the class that is being converted from. A non-trivial constructor is not required.

EDIT: I reworked the example to utilize a union since this is what you were asking about.

EDIT2: See below if you are trying to go the other way (construction) and don't want constructors.

#include <string>
#include <sstream>
using namespace std;

class FixedPoint
{
public:
    operator std::string() const
    {
        stringstream ss;
        ss << x_ << ", " << y_;
        return ss.str();
    }
    int x_, y_;
};

union Items
{
    FixedPoint point_;
    int val_;
};

int main()
{
    Items i;
    i.point_.x_ = 42;
    i.point_.y_ = 84;
    string s = i.point_;
}

If you're trying to go the other way -- eg, from an int to FixedPoint in my example -- then the normal way to do this is indeed to use a conversion constructor. Given that you don't want a non-trivial constructor, you have to resort to a conversion function.

FixedPoint make_fixed_point(int x, int y)
{
    FixedPoint ret;
    ret.x_ = x;
    ret.y_ = y;
    return ret;
}

union Items
{
    FixedPoint point_;
    int val_;
};


int main()
{
    Items i;
    i.point_ = make_fixed_point(111,222);
}
John Dibling
I think the OP asked about conversion constructors not conversion operators, i.e. he wants xy = <type> not <type> = xy
Eugen Constantin Dinca
@Eugen Constantin Dinca : Ah, thanks. Will update post.
John Dibling
A: 

Can't you just add a default constructor that'll allow it to be part of the union.

For example:

class bob
{
  public:
    bob(int a=0) { m_num = a; }
  private:
    int m_num;
};
void duck()
{
  union 
  {
    bob a;
  };
}

By giving the a=0 default, it should be able to be put in a union. I didn't try it myself, though.

miked
That is still a user defined constructor...
Eugen Constantin Dinca
You can't have objects with non-trivial constructors in unions. Perhaps you should have tried it yourself?
John Dibling
A: 

I am having a hard time at seeing what you would try to use this for. It seems smelly to have to constantly ensure that sizeof(FixedPoint) == sizeof(int) and, assuming that, there are other hidden gotchas, like endianness. Maybe I should back up a little bit here, unions only "convert" a value from one type to another in that it takes a chunk of memory and references it as a different type. i.e.

union BadConverter
{
    int integer;
    double fraction;
};

BadConverter.fraction = 100.0/33.0;
BadConverter.integer = ?;

I am pretty sure integer is not going to be 3, it is going to whatever the memory chunk of the double is that the integer bytes share with it.

Unions don't seem to be a very good fit for this sort of thing. I would think just defining a bunch of assignment operators from all the primitive types. i.e.

class FixedPoint
{
    FixedPoint& operator=(int value);
    FixedPoint& operator=(double value);
    ..etc..
    //Maybe something like this?
    template<typename T>
    FixedPoint& operator=(const T& value)
    {
        value = boost::lexical_cast<int>(value);
        return *this;
    }
}
messenger
Yeah..As for now, we've turned all "unions" into "structs" as a hack (for whatever uses fixed point)
Ootawata
@Ootawata: If you are satisfied with the answer you should accept it.
messenger