indexing overload concept idea

This is a discussion on indexing overload concept idea within the C++ Programming forums, part of the General Programming Boards category; This is a little weird, but which of these makes the most sense for an indexing operator for type "int" ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    indexing overload concept idea

    This is a little weird, but which of these makes the most sense for an indexing operator for type "int" (bear with me, I know this is weird)

    Say your number is

    classname x = 12345;

    x[0] returns 1 (first digit when converted using itoa) and x[1] returns 2

    or

    x[0] returns 5 (ones place) and x[1] returns 4 (tens place)

    or

    x[0] returns positive/negative, x[1] returns 5 (ones place) and x[10] returns 4 (tens place)

    The idea is you can get the digit in an int at a position... which makes the most sense / would be the most useful?
    Last edited by Trauts; 05-16-2003 at 07:57 PM.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    x[0] == ones column == 5
    x[1] == tens column == 4
    etc...

    That would be easiest to implement. But it all depends on what you need it for. The first idea may be better if that's what you intended your class to do.

    The third idea isn't good. Just have another member function return sign. That idea implies that the sign of the number is a digit; which isn't true.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    lol trauts, you are a wealth of curiousity. Too bad I am not a wealth of knowledge...

    Anyway, I recently made a similar object where you could use the subscript operator to obtain an individual digit of an int. I chose to make index 0 the ones-place digit, because 10 to the 0 power is 1, and I chose to make index the tens-place digit, since 10 to the 1 power is 10, and so on...but that's just my opinion

    I agree with ygf that you should jsut have a member that returns sign, or maybe overload a different operator(i don't know which one would be appropriate...bool maybe?)

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I agree with both of you.

    I agree with you about the curiosity... I take up a project and I don't give up, even when I think its impossible... then I find a way that almost does it :-)

    Hence the fact you'll notice most posts I've made in the past 2 months or so have been related to the "any" type, including this one :-)

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if you don't need to do math with the numbers, just use char boxes in your string... if you do need math, you can create a converter to convert these char's into int's and then do the math, but that's a little more complicated than some other ways...

    the way i suggested, the second two of your statements would work... you'd just have to check if x[0] was a '-' to make sure that x[1] is read as the 1's place... just shift everything down and make note of the negativity of the number somewhere...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Trauts
    I agree with both of you.

    I agree with you about the curiosity... I take up a project and I don't give up, even when I think its impossible... then I find a way that almost does it :-)

    Hence the fact you'll notice most posts I've made in the past 2 months or so have been related to the "any" type, including this one :-)
    A stringstream would make this class implementation simple. Also, it'd be nice to have implicit conversion to an integer (via operator int()) or even explicit, as in a member function to_int() or something. Another thing to think about trying is make the class a template, that way it could accept any type with an overloaded istream << operator (for the stringstream)

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    a soution?

    If I understood the problem, you could use intīs division.
    I implemented a little class that uses this concept
    Attached Files Attached Files
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just an idea >?<
    By Cgawd in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-30-2002, 09:08 AM
  2. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 01:26 PM
  3. Planned Development Community Idea (important)
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-12-2002, 01:13 PM
  4. Tutorial Idea
    By sean in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-30-2002, 11:26 PM
  5. totally screwed up idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-17-2001, 01:09 PM

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