Thread: Please check my C++

  1. #181
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, the standard says:
    13.5.3 Assignment
    An assignment operator shall be implemented by a non-static member function with exactly one parameter.
    It is possible to add a contructor to make Y assignable to X (in which case the constructor would do the hard work).

    Code:
    struct Y {};
    
    struct X
    {
        X() {}
        X(const Y&) {}
    };
    
    int main()
    {
        X x;
        Y y;
        x = y;
    }
    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).

  2. #182
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It should be equally possible to create an assignment operator in X that takes Y as argument.
    It just can't be global.
    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. #183
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Hello, read your posts...

    But anyway, it seems that you can't overload operator= for enum types anyway since it can only be overloaded as non-static member function.
    Operator = must be a member of the class to which it is defined to work with.
    As thus, operator = does not work for enums, only classes.
    Either way, you should not assign an int to an enum; you're essentially circumventing the whole C++ type safety.
    You can certainly make "global" operator=() operator, that takes two arbitrary classes [that you have some idea of how you would convert to the left-hand side type].

    The reason, I believe, for this operator is the need to serialize the data to file. If it was stored from the enum on the file, then it should be safe to read back. We could add checks in the operator to ensure that's the case.
    Am not sure if i have a solution to my problem....

  4. #184
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would say you have to cast the enum to int.
    But what's the code that uses the enum that relies on that assignment operator?
    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.

  5. #185
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    I would say you have to cast the enum to int.
    But what's the code that uses the enum that relies on that assignment operator?
    i figured there's not even a need to assign the int, cause i have the enums to assign anyway... No need for overloading. Thanx

    Car::TankStatus ts;

    // Ensure correct tank details
    if ( (ts>3) || (ts<0) ) {
    ts=Car::unknown;
    }

  6. #186
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    sorting

    My sort function works but not according to my desires. I have two fields that it needs to consider when sorting [car make & car model]... i.e If i have Mercedes Vito before Mercedes Benz in the list, then the two wont be sorted since both have the same make...

    this is how i use the sort function...
    Code:
    vector <Car> vehicle;
    sort(vehicle.begin(), vehicle.end());
    How do i modify this to achieve my desires?

  7. #187
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need a operator> (or is it operator<) implemented for your Car class. You would also, most likely, want to combine the effect of make and model, so for exampel Ford Escort is before Ford Focus. This would mean comparing the make first, and if they are equal, then compare the make component.

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

  8. #188
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    You need a operator> (or is it operator<) implemented for your Car class. You would also, most likely, want to combine the effect of make and model, so for exampel Ford Escort is before Ford Focus. This would mean comparing the make first, and if they are equal, then compare the make component.

    --
    Mats
    Cool, i already have that operator, just need to expand on it!
    Code:
    bool Car::operator<(const Car& car) const
    {
    	return make < car.make;
    }
    thnx... Sorry, how do you clear unwanted characters on stin again? is it cin.clear()?

  9. #189
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Cool, i already have that operator, just need to expand on it!
    Code:
    bool Car::operator<(const Car& car) const
    {
    	return make < car.make;
    }
    thnx... Sorry, how do you clear unwanted characters on stin again? is it cin.clear()?
    cin.ignore() perhaps? cin.clear() sets (or rather clears) the error flags, but doesn't affect input itself. So cin.clear() may be useful if you reach EOF in a file and then want to read it again.

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

  10. #190
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    thnx... Sorry, how do you clear unwanted characters on stin again? is it cin.clear()?
    http://cboard.cprogramming.com/showp...0&postcount=46
    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.

  11. #191
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Oh! that boost thing again... Lol! well i said am gonna return to you to explain it more, but didn't as yet... Let me try't then will flood you with questions

  12. #192
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Code:
    boost::integer_traits<int>::const_max
    Please explain, what does this have to do with STDIN, what's boost anyway?

  13. #193
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Code:
    boost::integer_traits<int>::const_max
    Please explain, what does this have to do with STDIN, what's boost anyway?
    It is simply a constant describing the largest positive value that an int can have, and nothing else. So it's really not necessary to use boost for this purpose - it's just a convenient place to get such a constant. It is also unlikely that you really need to ignore 2GB of input - most likely any number between 100 and 10000 would be perfectly find for NEARLY all purposes.

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

  14. #194
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Thanx mats for explaining boost..

    Code:
    bool Car::operator<(const Car& car) const
    {
    	return make < car.make;
    }
    Q.

    1. i know i wrote this but have a question... In the above, what is the different between make & car.make, is it a this pointer VS current object...?

    2. I tried doing this but doesn't work...

    Code:
    bool Car::operator<(const Car& car) const
    {
    	return (make < car.make) && (model < car.model);
    }
    Maybe because i don't understand how does find() use the operator '<'...

  15. #195
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you need is something like this:
    Code:
       return (a.x == b.x)? a.y < b.y : a.x < b.x;
    Or, if we use if-statement instead of the ternary operator:
    Code:
       if (a.x == b.x) return a.y < b.y;
       return a.x < b.x
    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM