Path: utzoo!attcan!uunet!know!cs.utexas.edu!milano!cadillac!vaughan@mcc.com From: vaughan@mcc.com (Paul Vaughan) Newsgroups: comp.lang.c++ Subject: Re: Transparent int class Message-ID: <12149@cadillac.CAD.MCC.COM> Date: 15 Oct 90 18:44:04 GMT References: Sender: news@cadillac.CAD.MCC.COM Reply-To: vaughan@mcc.com (Paul Vaughan) Distribution: comp Organization: MCC VLSI CAD Program Lines: 69 In-reply-to: matthew@hydra.ua.oz.au (Matthew Donaldson) I am not sure that this can be done in C++, but I am trying to make a class that looks exactly like an int (or as much so as possible), but inside works differently. I want an integer that is protected against concurrent reads and writes, but still looks like an int. So each operation would use mutual exclusion primitives to protect against concurrent assignment, etc. The aim is to be able to do something like this: ProtectedInt a; ... a= 2; cout << "Now a is " << a << "\n"; and everything else you can do with ints, such as a |= 2, fn(a), int b= a, ProtectedInt b=a, etc. [ as an aside, how are operations like |= handled? There is no operator|= ] I can do most of these - the main exception is the actual using of the class to return an int - that is, just using the "a" variable, and making it look like an int. I haven't found any way of doing this, and have had to be content with a.Value() or whatever, but this spoils the look of things a bit. Does anyone know if it is possible to do this, and if so how? Also, is there a more general solution to the whole problem, rather than having to define every operator for the class to perform operations on ints. By providing a constructor, conversion operator, assignment operators (the whole suite), and copy constructor, you can come pretty close to an integer look-alike. For instance, class Int { public: Int(int); Int(Int&); operator =(int); operator int(); operator +=(int); operator |=(int); private: int i; }; main() { Int i(3); i = 2; i += 3; i |= 4; int j = i; } compiles with cfront 2.0 and g++. I don't believe there is any way to get around having to define all the possible assignment operators (and there are quite a few for int's). Also, Int can't be made to behave *exactly* like ints because of the conversion rules--because it goes through an extra layer of conversion, there will be some differences when multiple conversions are required. Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639 Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan