Thread: Storing int and double type values under one variable name

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Storing int and double type values under one variable name

    I want to get inputs to a variable called “quantity”. The user can input it either in int or double types and the program needs to differentiate the type. Can I do it with C++?
    For the moment, I have used an abstract base class in which the proper type can be Input when the type is given.
    But what I want to do it keep both type under one particular variable in a Different class.
    Ex.

    Code:
    class Job
    {
    private:
    	int day;
    	Quantity quantity;
    	Work* work;
    	
    public:
    	Job(){}
    	~Job(){}
    };
    How to store both int and double types in this “quantity” variable?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Suppose you could store the value as a string and have different derived versions of Quantity return a double representation vs an int representation.

    There are a number of ways of going about this.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Can you please explain me one way of doing that.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming your data is of a basic type (float, double, int, etc) look up the notion of a special type of class called a union. The practical catch with a union is that you need to carry around an additional information that identifies what type of data is stored in the union, so it can be extracted correctly.

    There are a few constraints on unions. One of those constraints is that a C++ class type with non-trivial constructors, destructor, or assignment operator cannot be a member of a union (and nor can arrays of such objects). Unions can also not have base classes, nor be base classes.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    without initially identifying the type of data, is there a particular method to store both types under one variable?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Didn't he just mention a union?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Unions are bad. Review your requirements to make sure you really, really, really need to do what you're planning to do. Are you sure that a simple double doesn't suffice?

    If you still think you need the construct, use a Boost.Variant.

    http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When would "quantity" not be an integer? Usually, quantity would be a number of items, yes?

    One solution would of course be to use a double at all times, and just make the value "int" when you need to for some mathematical reason.

    Other alternatives involve using fixed point math.

    But if you (Thanuja91) can explain in a bit more detail what you want the quantity to represent, and why it needs to be double and int.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Unions are the C solution. They work, but they do not provide type safety, so if you store a double, but try to get an int, then you won't get the desired value. This can be resolved by having another variable that stores the type of the value in the union. Then when your accessor tries to read an int it can check to make sure the union really does store an int.

    Another solution is to have a base class pointer or reference. This requires a warper object, generally a template, that inherits from a generic object type. The disadvantage of this approach is that is is very verbose. You write a lot of code for something very simple.

    The best solution may be to use a pre-written implementation such as Boost::Variant.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    This is for a simulation purpose of a construction site. For ex. if there is a job named "removal" it can be given as number of pieces (in this case its an int) or in volume or area (in this case its double).

    To implement this I want to know whether there is a method.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you would select (at some point) whether the data is a "number of items" or "volume" or "area", right?

    This, to me, is a typical case where you would use two classes, something like this:
    Code:
    class Base 
    {
    private:
       double cost;    // Per item, volume or area.
    public:
       virtual double CostOf();
       virtual const double getCost() const { return cost;};
       virtual void setCost(const double aCost) {cost = aCost; };
    };
    
    class Items : public Base
    {
    private:
       int nItems;
    public:
       void SetItems(int aItems): nItems(aItems);
       virtual const double CostOf() const { return cost * nItems; };
    };
    
    class Items : public Base
    {
    private:
       int nItems;
    public:
       void SetArea(const double aArea): area(aArea);
       virtual const double CostOf() const { return cost * area; };
    };
    I'll leave it to you to imlement volume [should you wish to have a different class].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  5. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM