Author | Message | Time |
---|---|---|
GET_IN_MY_BELLY | why would you need this? can't you just put it all in the same thing? give me an example showing me that this can be useful for me. | January 4, 2004, 6:15 PM |
CupHead | It's useful when you have a function that needs to accept varying types of parameters, but don't want to create different functions for each data type. For instance, an application might be in a packet buffer class where you want to add data. Typically, you would need to do something like: [code] void PacketBuffer::InsertWORD(unsigned short Data) { // ... } void PacketBuffer::InsertDWORD(unsigned long Data) { // ... } [/code] With an implementation like: [code] PacketBuffer.InsertWORD (unsigned short)0x0001; PacketBuffer.InsertDWORD (unsigned long)0x00000001; [/code] With function overloading, you could do something like: [code] void PacketBuffer::Add(unsigned short Data) { // ... } void PacketBuffer::Add(unsigned long Data) { // ... } [/code] And then the implementation would be like: [code] PacketBuffer.Add (unsigned short)0x0001; PacketBuffer.Add (unsigned long)0x00000001; [/code] Basically it allows you to turn a common function with varying data types into a function able to be accessed with all data types rather than needing different function names for each data type you want to use. | January 4, 2004, 6:34 PM |
Arta | How about adding data to a packet? [code] void Add(DWORD Data) { // Add a dword } void Add(WORD Data) { // Add a word } void Add(LPSTR Data) { // Add a string } // Now you can use the same function for adding DWORDs, WORDs, and strings: DWORD d = 0; WORD w = 1; Add(d); Add(w); Add("abc"); [/code] | January 4, 2004, 6:36 PM |
Adron | I like to make different versions, like Addw(short word), Addi(int integer), Adds(char *string), because that way I don't have to manually cast to add an int as a short. | January 5, 2004, 12:18 AM |
iago | I have overloaded + non-overloaded. If I'm using a constant, I'll do addWord, addDWord, addByte, etc., and if I'm using variables I just use add(). It's really a matter of taste, though.. | January 5, 2004, 1:20 AM |