Operators for enumeration variables in class

xiaoxiao2021-04-05  272

A poker class, which members Suit delegate color (of course only four, so you can use enumeration type), you can assign a value to the Suit type (with = operator) in the program, SUIT can do self-increment operation (with operation symbol). code show as below:

// oloperty.cpp: Defines the entry point of the console application. #include "stdafx.h" #include using namespace std; class card {public: enum suit {diamonds, hearts, clubs, spades}; suit suit; // External accessible, you must define private: Suit & operator = (Suit & RS) // "=" Operator Overload {Suit = RS; RETURN RS;} Friend Const Suit & Operator (Suit & RS, int) // " " operator overload, here Friend must add {RETURN RS = ( SUIT) ((RS 1)% sizeOf (Suit);}};

INT _TMAIN (int Argc, _tchar * argv []) {card card; card.suit = card :: diamonds; std :: cout << card.suit << Endl; card.suit ; std :: cout << Card. Suit << Endl; Return 0;}

Related Website: http://doc.readmen.com/search.asp? SCH =% D4% CB% CB% E3% B7% FB% D6% D8% D4% D8HTTP: //www.vwind.com/dev/ HTML / 28.htm

转载请注明原文地址:https://www.9cbs.com/read-132034.html

New Post(0)