[Dev-C++] Operators involving smart pointers
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Frans E. <fra...@te...> - 2005-11-07 16:54:42
|
Hello all, I have a design problem which I don't know how to solve the best way. In my code I almost exclusively handle heap allocated objects, and accesses them via reference counting pointers(the counting is in the object, not the pointer). The problem emerges when invoking operators between objects. This looks for example like this[1]: Cardinality::Ptr(&(*opCard | *Cardinality::zeroOrOne)))); Evidently, this is not very pretty, when compared to the expression: opCard | Cardinality::zeroOrOne I'm taking the "| operator" as a representative example of my general problem. It's signature looks like this: Cardinality &operator|(const Cardinality &other) const; I wonder, should I add/change operators such that using the classes are more convenient? For example, I could change the operator to: Cardinality::Ptr &operator|(const Cardinality::Ptr &other) const; and thus reduce "Cardinality::Ptr(&(*opCard | *Cardinality::zeroOrOne))));" into "*opCard | Cardinality::zeroOrOne". Another alternative is to add in the default namespace: inline Cardinality::Ptr operator(const Cardinality::Ptr &o1, const Cardinality:Ptr &o2) /* ... */ which probably is the cleanest solution, and would result in "opCard | Cardinality::zeroOrOne" What makes me wonder is that I don't want to couple a smart pointer, which is more of a tool for memory management, to my classes(although they wouldn't live without the smart pointer). Somehow, I want to keep it modularized and separated. On the other hand, "Cardinality::Ptr(&(*opCard | *Cardinality::zeroOrOne))))" is very ugly, and perhaps a freestanding operator involving Cardinality::Ptr is modularized enough. What do people think? What is the cleanest solution? pros/cons? Cheers, Frans 1. Nope, I'm not defeferencing a temporary object because the | operator returns a (valid) reference. |