Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
Member Login

Posted: 11/26/2014 10:41:11 AM EDT
When you have functions that are related to a specific struct, which do you feel is the better approach:

1. Function pointer inside the struct. Function gets invoked such as:
mystruct->myfunc(mystruct, arg1, arg2);


2. Function outside the struct. I use static structs for pseudo namespacing, so it would look like this:
myns.myfunc(mystruct, arg1, arg2);


Might as well put generator/factory and cleanup functions in the ns as well.

CoolStruct * mystruct = myns.new_CoolStruct();

myns.myfunc(mystruct, arg1, arg2);
myns.del_CoolStruct(mystruct);



Link Posted: 11/26/2014 10:56:39 AM EDT
[Last Edit: 11/26/2014 11:28:43 AM EDT by NightFAL]
Option #2 looks cleaner. However, option #1 makes it much easier to change the struct's related function on the fly:

typedef struct coolstruct CoolStruct;

struct coolstruct {
char ** keyset1;
char ** keyset2;
bool ok;
char * (*transform)(CoolStruct *, int);
};


// Yes, I realize sizeof is an operator - not a function.
// I usually use parens with sizeof just for clarity.
CoolStruct * mystruct = malloc(sizeof(CoolStruct));

// Some stuff happens here to assign mystruct->keyset1
// and mystruct->keyset2.


// Now we make a decision based on some arbitrary criteria.
if (mystruct->keyset1[0][0] == '~') {
mystruct->transform = some_function;
}
else {
mystruct->transform = some_other_function;
}



Now we can throw mystruct around like a madman and other code can safely invoke mystruct->transform(mystruct, 87) without worrying about whether the correct transform function will be used.

Link Posted: 11/26/2014 3:18:43 PM EDT
[Last Edit: 11/26/2014 3:21:52 PM EDT by JosephK]
If you are doing actual C programming and not C++, traditionally the struct has no functions encapsulated in it, so option #2 would be the expected coding style.

Yes, I realize that today's C++ compilers let you do C coding using structs like C++ classes. Call me a traditionalist.

OK, after rereading what you are doing, the function pointer inside the struct does have its advantages as you are implementing something similar to C++ virtual functions. However, at that point, one has to question why you are not using C++ to start with.
Link Posted: 11/26/2014 3:24:36 PM EDT
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By JosephK: However, at that point, one has to question why you are not using C++ to start with.
View Quote



Because C++ is a blight on humanity, an abomination, and causes cancer.



Link Posted: 11/26/2014 4:15:10 PM EDT
I should note that I jumped on the C++ bandwagon around 20 years ago.

After spending some time with Smalltalk, Eiffel, CLOS (Common LISP Object System), and a few others, my views on OO languages changed a bit and I became disenchanted with C++.

If I do any OOP lately, it is usually in Python, Java, or C#.

I also now favor using mix-ins/traits over inheritance and emphasize has-a instead of is-a when possible.


Link Posted: 11/27/2014 11:25:52 PM EDT
[Last Edit: 11/27/2014 11:27:08 PM EDT by MrHiggs]
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By NightFAL:
I should note that I jumped on the C++ bandwagon around 20 years ago.

After spending some time with Smalltalk, Eiffel, CLOS (Common LISP Object System), and a few others, my views on OO languages changed a bit and I became disenchanted with C++.

If I do any OOP lately, it is usually in Python, Java, or C#.

I also now favor using mix-ins/traits over inheritance and emphasize has-a instead of is-a when possible.


View Quote



How can you become disenchanted with C++? It's arguably the most powerful, most flexible language available. I do heavy data analysis. Most of our code is python driven C++. With this format, there isn't much you can't do. I prefer python now for anything small, but if I need to do something serious, I go to C++.

Eta OP just read your username - nicely done
Top Top