Thread: Unsigned/signed classes?

  1. #1
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    Unsigned/signed classes?

    Is it possible to design a user-defined data type that the designer can also dictate its behavior if declared signed or unsigned (by the user)?

    Is it even possible to say something like:

    Code:
        myClass objName;
    and have the option of:

    Code:
        unsigned myClass objName;
    ???

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    use templates.

    --
    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.

  3. #3
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Quote Originally Posted by matsp View Post
    use templates.

    --
    Mats
    I want to create a 512 -bit data type, this is why I ask...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >unsigned myClass objName;
    Not really, but you can get close with template specialization:
    Code:
    enum signedness { SIGNED, UNSIGNED };
    
    template <signedness T>
    class my_class {};
    
    template<>
    class my_class<SIGNED> {
    public:
      my_class() { std::cout<<"signed my_class\n"; }
    };
    
    template<>
    class my_class<UNSIGNED> {
    public:
      my_class() { std::cout<<"unsigned my_class\n"; }
    };
    
    int main()
    {
      my_class<SIGNED> foo;
      my_class<UNSIGNED> bar;
    }
    My best code is written with the delete key.

  5. #5
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    thanx, that should help a bit, no pun intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM