Question

I'm trying to create an object that can be of any type. Here's the code:

#include <stdio.h>

class thing
{
public:
    void *p;
    char type;

    thing(const char* x)
    {
        p=(char*)x;
        type=0;
    }

    thing(int x)
    {
        p=(int*)x;
        type=1;
    }

    thing(bool x)
    {
        p=(bool*)x;
        type=2;
    }

    /*
    thing(float x)
    {
        p=(float*)x;
        type=3;
    }
    */

    void print()
    {
        switch(type)
        {
        case 0:
            printf("%s\n", p);
            break;
        case 1:
            printf("%i\n", p);
            break;
        case 2:
            if(p>0)
                printf("true\n");
            else
                printf("false\n");
            break;
        case 3:
            printf("%f\n", p);
            break;
        default:
            break;
        }
    }
};

int main()
{
    thing t0("Hello!");
    thing t1(123);
    thing t2(false);

    t0.print();
    t1.print();
    t2.print();

    return 0;
}

Code is working and when I run the program, it displays:

Hello!
123
false

But if I uncomment the float constructor, the compiler writes the following error:

main.cpp: In constructor 'thing :: thing (float)': main.cpp: 30:13:
error: invalid cast from type 'float' to type 'float *'

Why is it not working for float type? I use: Windows XP SP3, MinGW GCC 4.7.2.

Was it helpful?

Solution

You should not be casting from random types to pointer types. Even though casting char const *, int, and bool appear to work for you, they are not any more what you want than casting float to a pointer. In fact you should view any cast in C++ as a warning sign that you may be doing something incorrectly.

Instead you should do something like the following.

class thing {
private:
  union {
    char const *cs;
    int i;
    bool b;
    float f;
  };
  enum class type { cs, i, b, f } stored_type;

public:

  thing(const char* x) : cs(x), stored_type(type::cs) {}
  thing(int x)         :  i(x), stored_type(type:: i) {}
  thing(bool x)        :  b(x), stored_type(type:: b) {}
  thing(float x)       :  f(x), stored_type(type:: f) {}

  void print()
  {
    switch(stored_type)
    {
    case type::cs:
      std::printf("%s\n", cs); break;
    case type::i:
      std::printf("%i\n", i); break;
    case type::b:
      std::printf("%s\n", b ? "true" : "false"); break;
    case type::f:
      std::printf("%f\n", f); break;
    }
  }
};

Or better yet you could use a library that already does this for you, such as boost::variant, or boost::any.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top