Extending basic data types.

This is a discussion on Extending basic data types. within the C++ Programming forums, part of the General Programming Boards category; Operator overloading abstract data types is all dandy but is the same thing possible with basic data types. For instance, ...

  1. #1
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39

    Extending basic data types.

    Operator overloading abstract data types is all dandy but is the same thing possible with basic data types. For instance, can I add a custom '+' operator to 'char' or redefine it for 'int'?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    No, as far as I know. In C++, the primitive data types are usually implemented directly, not as classes.

    You could, however, use a hack like this:
    Code:
    #define int int_class
    Why do you want to do that? You could always define your own my_char class that does what you want. Redefining the behaviour of the primitive types would be very confusing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31
    The answer is yes, but it will take more lines of code than you expect. You create a class with the basic type you are modifying as a private member, and then add all the functionality you want to the class as overloaded functions. Some has almost certainly already done this once, so a search for a template that already implements some basic class would be a good starting point.

  4. #4
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by dwks View Post
    Why do you want to do that? You could always define your own my_char class that does what you want. Redefining the behaviour of the primitive types would be very confusing.
    The reason is that so I might add for example a '+' operator to 'char' and when doing "char + char" or "char * + char *" it will return a 'char *' containing both. Since these types are invalid for '+', I would like to add them some way.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    So, what does "char + char" do? Once you come up with a name for this operation, write a function with that name that takes two char arguments.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Neither char + char nor char* + char* are invalid. They just don't do what you expect.

    Operator overloading was originally meant for extending the existing semantics of operators to new data types. It was certainly not meant to change the semantics for existing data types. (Changing the semantics for new data types happens and can be useful, but must be used with great care.)
    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

  7. #7
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by CornedBee View Post
    Neither char + char nor char* + char* are invalid. They just don't do what you expect.
    g++ seems to think so atleast:

    Code:
    $ g++ test.cpp 
    test.cpp: In function ‘int main()’:
    test.cpp:6: error: invalid operands of types ‘char*’ and ‘char*’ to binary ‘operator+’

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    That's because operator+ for a pointer is valid only with an integral or enumeration type, not another pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I wonder how much you know about std::string? It already is quite good at concatenating std::strings, char's and char*'s.

    For example, if you want to concatenate char*'s and char's, you might do it like this:
    Code:
    char* hello = "Hello";
    char* world = "world";
    std::string result = std::string(hello) + ' ' + world + '!';
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by anon View Post
    I wonder how much you know about std::string? It already is quite good at concatenating std::strings, char's and char*'s.

    For example, if you want to concatenate char*'s and char's, you might do it like this:
    Code:
    char* hello = "Hello";
    char* world = "world";
    std::string result = std::string(hello) + ' ' + world + '!';
    Strings are fine, except I want it to return a char *.

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You can get a string's char* by calling it's c_str() method

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> Strings are fine, except I want it to return a char *.
    I think the point was that you should use strings pretty much everywhere.

    Note that c_str() returns a const char*, so you can't modify the C style string it returns. If you really do need a char* that can be modified, then you would have to copy the return value of c_str() into your char* with strcpy. Of course, if you're going to do that, you might as well not use strings at all and just do everything the C way and do the concatenation with strcat.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If you want a mutable char*, use a vector<char> and do &v[0].
    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

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by CornedBee View Post
    If you want a mutable char*, use a vector<char> and do &v[0].
    Which, annoyingly, is not guaranteed to work, but it does in practice. It will NOT work for a deque.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It is guaranteed to work, as of the 2003 minor revision of the C++ standard.

    But ONLY for vector. Not for list or deque or anything else.
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function that takes diff data types as paramter
    By nepdude in forum C Programming
    Replies: 7
    Last Post: 07-06-2007, 01:47 PM
  2. What are abstract data types
    By bhagwat_maimt in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 09:43 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

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