Results 1 to 6 of 6

Thread: An object's data type

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60

    An object's data type

    Don't know if what I'm about to say will make sense, so bear with me and ask if you don't understand...

    When you have a class, in this case an 'Multiplier' class, how can you using the object identifier to return a value?

    Example:

    {
    ____Mulitiplier Multi;

    ____Multi.Number1=2;
    ____Multi.Number2=4;

    ____cout << Multi; *
    }

    This would return 8. I realise it's a useless class, but I'm more concerned about the * line. How could I make this line print the value '8' without having to use a line akin to "cout << Multi.result;"?

    Does it have anything to do with class operators?

  2. #2
    Junior Member MikeyD's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    17
    yes, it has something to do with an operator - in this case it's maybe the int-op.
    Code:
    class Multi
    {
    public:
    	operator int() {return No1 * No2;}
    	int No1, No2;
    };
    Multi oMulti;
    oMulti.No1 = 2;
    oMulti.No2 = 4;
    cout << oMulti; // 8
    Mikey

  3. #3

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60
    Congratulations x 2!

    #1 for solving the problem
    #2 for working out what I was trying to say!

    Thanks muchly!

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Tip: use code tags. Then you don't need long lines of undescores to keep your code indented.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60
    ... Not entirely sure how code tags work. Care to elaborate?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    [ code]
    Your code here
    [/ code]

    Remove the spaces.

    Then your code will preserve formatting:
    Code:
    int main()
    {
        // indentation
        printf("Hello, World!");
        return 0;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width