473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

define class constants in C++

I am designing a class of 144 bit floats, and I can't figure out how to create a constant with class scope.
Here is the relevant information:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <endian.h>
  5. #include <stdint.h>
  6.  
  7. #if BYTE_ORDER == 1234
  8. #define N0of2 0
  9. #define N1of2 1
  10. #elif BYTE_ORDER == 4321
  11. #define N0of2 1
  12. #define N1of2 0
  13. #endif
  14.  
  15. union UINT128 {
  16.   uint64_t d[2];
  17.   uint32_t i[4];
  18.   uint16_t s[8];
  19.   uint8_t c[16];
  20. }  __attribute__((packed)) ;
  21.  
and
Expand|Select|Wrap|Line Numbers
  1. struct F144S {
  2. #if __FLOAT_WORD_ORDER==1234
  3.   UINT128 mant;
  4.   int16_t exp;
  5. #elif __FLOAT_WORD_ORDER==4321
  6.   int16_t exp;
  7.   UINT128 mant;
  8. #endif
  9. } __attribute__((packed)) ;
  10.  
  11. #include <ctype.h>
  12.  
  13. class float144 {
  14. public:
  15. union f_144 {
  16.   uint8_t c[18];
  17.   uint16_t s[9];
  18.   struct F144S f; } fl;
  19. float144() { } 
  20. float144(f_144 x) { fl=x; }
  21. // XXXX
  22. };
  23.  
If I replace // XXXX with
float144 ten1() { fl.f.exp=(0x4002); fl.f.mant.d[N1of2]=0x4000000000000000; fl.f.mant.d[N0of2]=0; return fl; }
I get something that can serve as a constant, but I don't like carrying around the parentheses. I want something like
static const float144 ten1 { ten1.f.exp=0x4002; ten1.f.mant.d[N1of2]=0x4000000000000000; ten1.f.mant.d[N0of2]=0; };
but after 2 days, I've been unable to find anything the gcc compiler will accept.
Jun 16 '10 #1
2 2498
Banfa
9,065 Expert Mod 8TB
You mean something like this

Expand|Select|Wrap|Line Numbers
  1. // Header
  2. class example
  3. {
  4. public:
  5.    example(int init) :
  6.    m_Data(int)
  7.    {
  8.    }
  9.  
  10.    ~example()
  11.    {
  12.    }
  13.  
  14.    static const example e10;
  15.  
  16. private:
  17.    int m_Data;
  18. };
  19.  
  20. // Source File
  21. const example example::e10(10);
  22.  
  23.  
Jun 16 '10 #2
Thanks, you gave me the clue I needed to make progress. I made a new constructor, and was able to do what I wanted, although not within the class definition. Later, I learned that (at least until C++0x) static const in-class initialization is only allowed with integers. To get what I want without having to learn all about templates, I'm going to have to use #define and #undef. Note that the data I'm trying to initialize is a struct in a union in a class, and the grammar to do that is (I think) in some cases ambiguous.
Jun 20 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Henry | last post by:
I have a large number of user define class objects and want display in a datagrid and able to perform paging and column sorting. It seems using DataView is the easiest way but the objects are not...
1
by: PengYu.UT | last post by:
Hi, The following program(doesn't compile) shows that I want to define class A with different template arguments "int" and "double". Because class A with int or with double are very different, I...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
29
by: Michael D. Ober | last post by:
Is there any way to create a constant in a class that can be used both with an instantiated object and without. For example: dim ClassConst as string = myClass.ConstantString dim myObj = new...
13
by: Allen | last post by:
I defines FileLogConstant class as following. class FileLogConstant { public: static const INT32 REGISTER_LOGGER; static const INT32 REGISTER_METHOD; static const INT32 MAX_LOGGER_COUNT;
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
2
by: Alex Weber | last post by:
weird issue i ran into today... its possible to define case-insensitive constants outside the scope of a class using: define('name', 'value', false); however, you cannot use define() for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.