Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1103825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:23:43+00:00 2026-05-17T01:23:43+00:00

What I’m trying to do is create a new custom data type that behaves

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:23:44+00:00Added an answer on May 17, 2026 at 1:23 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I know there's a lot of other questions out there that deal with this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.