how to make our own data type ?

This is a discussion on how to make our own data type ? within the C++ Programming forums, part of the General Programming Boards category; hello , i have a question . how can i make a new data type! for example i want to ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    how to make our own data type ?

    hello , i have a question .
    how can i make a new data type!
    for example i want to make a data that is capable of holding 3 char instead of 1 char .
    for example see the following codes , im planning to do such a thing! if it is not possible how can i achieve my plan in this case?
    storing a label in a label register (each element of label register is capable of holding a 3 characters (despite char that each element represents one character) ,so that whenever i need a specific label name , by referring to labelregister[index]. i can retrieve the name!
    i tried using :
    #define mychar char[3]
    to make new data type capable of holding my labels ! but no luck!

    mychar char[3];
    mychar label[15];

    char name[3];

    label[0]=name;

    how to do such a thing?

    regards
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Why do you need a data-type to store 3 chars? Usually, there might be a better solution.
    Otherwise, you'd want a class to emulate your needs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Elysia View Post
    Why do you need a data-type to store 3 chars? Usually, there might be a better solution.
    Otherwise, you'd want a class to emulate your needs.
    well as i mentioned before , im trying to have a register (an array ) that holds the labels(names) entered by user ,so that when i need a name , i can simply use index of that register and retrieve the name!
    what are the other solutions?
    and what about class !
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    It sounds like you need a map, and conveniently there is one in the STL: #include <map>

    There is a tutorial on the website which can introduce the use of the class,
    http://www.cprogramming.com/tutorial/stl/stlmap.html

    for other issues see a reference:
    http://www.dinkumware.com/manuals/?m...&page=map.html

    With a little planning you should be ok.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Why don't you just use string ?
    std::string is very good for your purpose.

    But if you really need to define a type:
    Code:
    typedef MYSTRING std::string;
    
    MYSTRING wow;
    wow = "hello";
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    What you might want is:
    std::map<std::string, int> register;

    So you can save your names in a register looked-up by index.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by citizen View Post
    It sounds like you need a map, and conveniently there is one in the STL: #include <map>

    There is a tutorial on the website which can introduce the use of the class,
    http://www.cprogramming.com/tutorial/stl/stlmap.html

    for other issues see a reference:
    http://www.dinkumware.com/manuals/?m...&page=map.html

    With a little planning you should be ok.
    Quote Originally Posted by execute View Post
    Why don't you just use string ?
    std::string is very good for your purpose.

    But if you really need to define a type:
    Code:
    typedef MYSTRING std::string;
    
    MYSTRING wow;
    wow = "hello";
    Quote Originally Posted by Elysia View Post
    What you might want is:
    std::map<std::string, int> register;

    So you can save your names in a register looked-up by index.
    all of you dears many tanx . i really appreciate your answers
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Dimension Data Type
    By ivandn in forum C Programming
    Replies: 1
    Last Post: 01-27-2005, 05:02 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21