diff options
| author | Paul Buetow <paul@buetow.org> | 2021-05-31 10:09:19 +0100 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-05-31 10:09:19 +0100 |
| commit | d3a70f706d57530e6c3a12364af0fdcf51ec6e20 (patch) | |
| tree | 2eb8d872ee3ae5254850c4cc9e2f3372659f594e /gemfeed/2016-11-20-methods-in-c.html | |
| parent | c7d03dc1b79d2214db40e322a31d1844b1c64d87 (diff) | |
Publishing new version
Diffstat (limited to 'gemfeed/2016-11-20-methods-in-c.html')
| -rw-r--r-- | gemfeed/2016-11-20-methods-in-c.html | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/gemfeed/2016-11-20-methods-in-c.html b/gemfeed/2016-11-20-methods-in-c.html index 4187e741..02677add 100644 --- a/gemfeed/2016-11-20-methods-in-c.html +++ b/gemfeed/2016-11-20-methods-in-c.html @@ -50,9 +50,9 @@ h2, h3 { <body> <h1>Methods in C</h1> <p class="quote"><i>Written by Paul Buetow 2016-11-20</i></p> -<p>You can do some sort of object oriented programming in the C Programming Language. However, that is very limited. But also very easy and straight forward to use.</p> +<p>You can do some sort of object-oriented programming in the C Programming Language. However, that is very limited. But also very easy and straightforward to use.</p> <h2>Example</h2> -<p>Lets have a look at the following sample program. Basically all you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer:</p> +<p>Let's have a look at the following sample program. All you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer:</p> <pre> #include <stdio.h> @@ -86,7 +86,7 @@ int main(void) { printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); } </pre> -<p>As you can see you can call the function (pointed by the function pointer) the same way as in C++ or Java via:</p> +<p>As you can see, you can call the function (pointed by the function pointer) the same way as in C++ or Java via:</p> <pre> printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b)); printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); @@ -105,13 +105,13 @@ Division(3.000000, 2.000000) => 1.500000 </pre> <p>Not complicated at all, but nice to know and helps to make the code easier to read!</p> <h2>The flaw</h2> -<p>That's actually not really how it works in object oriented languages such as Java and C++. The method call in this example is not really a method call as "mult" and "div" in this example are not "message receivers". What I mean by that is that the functions can not access the state of the "mult" and "div" struct objects. In C you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument:</p> +<p>However, that's not really how it works in object-oriented languages such as Java and C++. The method call in this example is not a method call as "mult" and "div" in this example are not "message receivers". I mean that the functions can not access the state of the "mult" and "div" struct objects. In C, you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument:</p> <pre> mult.calculate(mult,a,b)); </pre> -<p>How to overcome this? You need to take it further...</p> +<p>How to overcome this? You need to take it further.</p> <h2>Taking it further</h2> -<p>If you want to take it further type "Object-Oriented Programming with ANSI-C" into your favorite internet search engine, you will find some crazy stuff. Some go as far as writing a C preprocessor in AWK, which takes some object oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is actually similar to how the C++ language had its origins.</p> +<p>If you want to take it further, type "Object-Oriented Programming with ANSI-C" into your favourite internet search engine, you will find some crazy stuff. Some go as far as writing a C preprocessor in AWK, which takes some object-oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is similar to how the C++ language had its origins.</p> <p>E-Mail me your thoughts at comments@mx.buetow.org!</p> <a class="textlink" href="../">Go back to the main site</a><br /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
