Click here to Skip to main content
15,885,743 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to pass a window handle to another as command line argument. Pin
jschell21-May-11 12:22
jschell21-May-11 12:22 
AnswerRe: How to pass a window handle to another as command line argument. Pin
Mark Salsbery21-May-11 15:54
Mark Salsbery21-May-11 15:54 
Question[WINSOCK2] Handling icmp port unreachable Pin
Member 296547120-May-11 22:20
Member 296547120-May-11 22:20 
AnswerRe: [WINSOCK2] Handling icmp port unreachable Pin
Member 296547123-May-11 6:41
Member 296547123-May-11 6:41 
QuestionHow to return different types within one function Pin
Cold_Fearing_Bird20-May-11 19:29
Cold_Fearing_Bird20-May-11 19:29 
AnswerRe: How to return different types within one function Pin
Richard MacCutchan20-May-11 22:04
mveRichard MacCutchan20-May-11 22:04 
AnswerRe: How to return different types within one function Pin
jschell21-May-11 12:27
jschell21-May-11 12:27 
AnswerRe: How to return different types within one function Pin
Stefan_Lang23-May-11 2:01
Stefan_Lang23-May-11 2:01 
As jschell said, the only way this would even work is if you make sure that classA and classB are derived from T, otherwise the return type is wrong.

Calling the 'right' operator is only possible for the compiler if it can derive the type from the function call, either by analyzing the arguments, or by explicitely stating the type. Without arguments, you have to do the latter, and that is only possible by using the full operator name like this:
handle h;
classA* a = h::operator-> <classA> ();

That said, there are multiple design mistakes:

1. When you overload an operator, anyone taking advantage of this operator will implicitely assume that it behaves similarly as the same operator for does for other objects. In case of operator->(), the standard behaviour is 'dereferencing', i. e. delivering a reference to on object that is encapsulated within the original object. Basically, what you did is like calling a function 'add', when in truth it performs a multiplication.

2. You should always strive to indicate when a function returns an object that is created on the heap, and thus needs to be properly deallocated again. Otherwise there is a high risk that anyone else who'll work with your class in the future will forget to delete the allocated resources later. Function names like 'CreateXY', or 'Copy', indicate that a new object is being created, and you need to take care to destroy it. The dereferencing operator is usually interpreted more as a 'Get' function, indicating that its resource management is handled by someone else.

3. There's no point at all in overloading an operator, when you always have to write it explicitely to be able to invoke it. Use an explicit, normal function name instead!

4. Your class handle serves in some way as an interface to a limited number of specific classes. Unless these classes may or should be mixed up within a single handle object, it's probably a good idea to make this a template class. This would also eliminate your problem, and the need for the type indicator m_i
template <class T>
class handle {
   T* operator->() {
      return new T;
   }
};
class classA {
public:
  doWork();
};
// ...
   handle<classA> hA;
   hA->doWork(); // calls classA::doWork(), but produces a memory leak!
   classA* a = hA.operator->(); // we need the full operator name for this
   a->doWork(); // again, calls classA::doWork as above
   delete a; // clean up, thanks to the reference we stored


I suspect that you wanted to write something like hA->doWork();, but in doing so you'd produce a memory leak. The only way to avoid that leak would be storing the reference, but that requires calling the full operator name, negating any advantages that you might have hoped to gain from overloading an operator in the first place. If I am correct, then what you need is not a handle, but a class hierarchy with virtual functions.
QuestionFunction pointer problem Pin
.jpg20-May-11 6:13
.jpg20-May-11 6:13 
QuestionRe: Function pointer problem Pin
David Crow20-May-11 7:16
David Crow20-May-11 7:16 
AnswerRe: Function pointer problem Pin
Albert Holguin20-May-11 8:08
professionalAlbert Holguin20-May-11 8:08 
GeneralRe: Function pointer problem Pin
David Crow20-May-11 8:12
David Crow20-May-11 8:12 
GeneralRe: Function pointer problem Pin
Albert Holguin20-May-11 9:24
professionalAlbert Holguin20-May-11 9:24 
AnswerRe: Function pointer problem Pin
smags1320-May-11 7:43
smags1320-May-11 7:43 
GeneralRe: Function pointer problem Pin
Albert Holguin20-May-11 10:02
professionalAlbert Holguin20-May-11 10:02 
AnswerRe: Function pointer problem Pin
Paul Michalik20-May-11 23:10
Paul Michalik20-May-11 23:10 
QuestionSearch for bytes array in a binary file Pin
Smart Arab20-May-11 5:39
Smart Arab20-May-11 5:39 
AnswerRe: Search for bytes array in a binary file Pin
Albert Holguin20-May-11 5:47
professionalAlbert Holguin20-May-11 5:47 
GeneralRe: Search for bytes array in a binary file Pin
Smart Arab20-May-11 6:04
Smart Arab20-May-11 6:04 
GeneralRe: Search for bytes array in a binary file Pin
David Crow20-May-11 7:54
David Crow20-May-11 7:54 
AnswerRe: Search for bytes array in a binary file [modified] Pin
Albert Holguin20-May-11 7:54
professionalAlbert Holguin20-May-11 7:54 
QuestionHow do I update a Form from a function? Pin
Freddie Code20-May-11 4:53
Freddie Code20-May-11 4:53 
AnswerRe: How do I update a Form from a function? Pin
Freddie Code20-May-11 6:43
Freddie Code20-May-11 6:43 
Questiondamage after normal block( #24094) Pin
Sakhalean20-May-11 0:45
Sakhalean20-May-11 0:45 
AnswerRe: damage after normal block( #24094) Pin
Chris Losinger20-May-11 0:56
professionalChris Losinger20-May-11 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.