Thread: Wouldn't this be about the same as a class in C++?

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    Wouldn't this be about the same as a class in C++?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define private const
    
    
    typedef struct class
    {
        int (* method1)(int);
        int (* method2)(int);
    
    
        private int x;
        private int y;
    } class;
    
    
    inline int mult_by_2(int num)
    {
        return num * 2;
    }
    
    
    inline int add_one(int num)
    {
        return ++num;
    }
    
    
    int main( )
    {
        private class class_instance = {add_one, mult_by_2, 10, 5};
    
    
        fprintf(stdout, "%d, %d", class_instance.method1(class_instance.x), class_instance.method2(class_instance.y));
    
    
        return 0;
    }
    If it is, then I don't see the point of having them in the newer languages.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by HelpfulPerson
    Wouldn't this be about the same as a class in C++?
    I'm not sure how to address this. I suspect that you're thinking purely from the perspective of syntax.

    The notion of const-ness is different from the notion of private access to members, so your #define does not make sense. To make a struct implementation private in C, the way that I would normally reach for is to use the opaque pointer idiom to hide it (which can also be used in C++, where it is also known as pimpl: pointer to implementation).

    You defined a struct with member function pointers. This might be a precursor to implementing inheritance, but the way you're using it now is rather unnecessary: it is just to mimic C++'s member function call syntax, but that does not provide any value by itself.

    Quote Originally Posted by HelpfulPerson
    If it is, then I don't see the point of having them in the newer languages.
    To provide a higher level of abstraction, to remove boilerplate, to have certain syntax that the language designer felt would be helpful to programmers in expressing some aspect of the (object oriented programming) paradigm, to allow compilers to better perform optimisations, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If it is, then I don't see the point of having them in the newer languages.
    The point is easy, built-in support for object-oriented programming rather than kludging your way through. There are certainly ways to do OOP in C, yours (some issues notwithstanding) is one of them, but it is not as simple or elegant as newer languages provide. You'd also find it hard, but not impossible, to do things like inheritance (especially multiple inheritance / interfaces) with this kind of paradigm. It's not that it can't be done, it's that it's a lot of work to do it right.

    Back in the day when I wrote WinAPI code, I could do anything I now do in C#, but sometimes what used to take four hundred lines can now be done in four lines. I haven't expanded what I can do, I've made doing the same things much easier.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by HelpfulPerson View Post
    If it is, then I don't see the point of having them in the newer languages.
    Well, then, you can rest easy. Your code is not equivalent to a class (despite your usage of related identifiers).

    The fact you think your sample is equivalent means you have a very narrow notion of what a C++ class is.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by grumpy View Post
    Well, then, you can rest easy. Your code is not equivalent to a class (despite your usage of related identifiers).

    The fact you think your sample is equivalent means you have a very narrow notion of what a C++ class is.
    I was asking if it was similar to or close to a class in C++, I never said I had any experience in the language or that I thought it was equivalent.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    There's no point using C++ if all you need is objects with private members and functions that operate on them. C is easier to read, and actually more flexible, as you can add functions which need additional dependencies in independent files.

    But if you need polymorphism, that is member function pointers which change, C rapidly gets unwieldy. You got to write code to "query for an interface", select the right function, call it with precisely the right pointer. Then everything tends to need to be passed about as a void *, so you've got to be careful to know what to cast it back to, and it's hard to debug.

    C++ has its place. But it's often used unnecessarily, in programs which ought to be written in C.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Malcolm McLean View Post
    There's no point using C++ if all you need is objects with private members and functions that operate on them. C is easier to read, and actually more flexible, as you can add functions which need additional dependencies in independent files.

    ...

    C++ has its place. But it's often used unnecessarily, in programs which ought to be written in C.
    No. You are wrong. That is all I'm going to say (because it's so wrong in so many ways, I do not even know where to begin).
    The fact that you think that C++ programs should be written in C shows how twisted your mind is, whether that is intentional or not.
    Go read a beginner's book on C++ and check back here later. Unless you do that, I will consider you qualified to utter such a statement.
    Oh, and go write a program in C++ using what you learned in the book. I would this list.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Malcolm's point is mostly opinion-based, but you can use the opaque pointer idiom in C to mimic the private access label. I would disagree with Mutant John, though, because what he needs to do in multiple files, I can do with a single word, "private", and I find that convenient.

    Still, don't do this:
    The fact that you think that C++ programs should be written in C shows how twisted your mind is, whether that is intentional or not.
    You're putting words in his mouth. The specific portions of the post that you quoted merely suggest that C++ is not the right tool for the job when all you need is information hiding. And indeed it might not be, depending on other people's expectations or your own preferences, if you have room to exercise them. The only way to say that certain C++ programs need to be written in C is to put it in those words, though.

    Go read a beginner's book on C++ and check back here later. Unless you do that, I will consider you qualified to utter such a statement.
    This level of arrogance winds me up a whole bunch, but I'll excuse it since you really didn't get the point.
    Last edited by whiteflags; 08-07-2013 at 07:28 PM.

  9. #9
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Oh look, this again.

    There's no point using C++ if all you need is objects with private members and functions that operate on them.
    There is no point in using an object-oriented language if you've decided you need objects and encapsulation?

    Suppose for a moment that you've decided a certain problem is best solved using higher-order functions, but no other features of functional programming are needed. Would you then reach for C as well?

    Do you regularly expose yourself to nasty function pointer / cheshire cat / void pointer hacks to achieve something which could be done trivially with another tool?

    C is easier to read, and actually more flexible
    C is more flexible than C++? You realize that C is a subset of C++ right? Any flexibility that C might have is also present in C++, only it has a tonne of additional features.

    As far as readability goes: If you don't like templates, just say so, there is no shame in that.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by whiteflags View Post
    This level of arrogance winds me up a whole bunch, but I'll excuse it since you really didn't get the point.
    I have to agree with Elysia here. HelpfulPerson clearly does not understand C++, and is therefore decidedly unqualified to make statements about its general usefulness. Perhaps it's not useful to HelpfulPerson, but the statement was made in reference to C++ and its usefulness in a general sense. This is made clear by the stated belief that many C++ programs that "ought to be written in C." The simple fact is that a program ought to be written in whatever language the programmer feels comfortable using, that offers the tools necessary to complete the task. HelpfulPerson is obviously ignorant of C++, and nobody is placing any blame for that, and it's hardly the point. A great deal of arrogance was shown by HelpfulPerson by deciding what language programs ought to be written in - programs over which he/she has no control or influence. It actually reminds me a lot of Linus Torvaalds' opinion of C++.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OK. Too bad Micheal McLean made the statement that Elysia disagreed with.

    A great deal of arrogance was shown by HelpfulPerson by deciding what language programs ought to be written in
    I don't think anywhere there was the statement that programs in general need to be C.
    Last edited by whiteflags; 08-07-2013 at 08:49 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    This level of arrogance winds me up a whole bunch, but I'll excuse it since you really didn't get the point.
    The thing is that Malcolm clearly does not seem to understand the value of C++, and still seems stuck in thinking in C terms. This does neither language any favors, and it is therefore that I believe that Malcolm should actually get a real grip of both languages before uttering opinions such as "C is more readable than C++" or "C++ has its place. But it's often used unnecessarily, in programs which ought to be written in C."
    Yes, there are places where C should be used instead of C++, but that generally has to do with portability and tool chains than other things. That quote seems to imply to me something similar to Torvalds, that some projects written in C++ should be written in C simply because Malcolm cannot see any use for C++, and that is nonsense.
    So sorry if it comes of as arrogant, but I gave a matter-of-factually speech in my opinion that Malcolm should learn C++ before uttering such things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Don't apologize to me. I'm just reacting to a fantastic degree of academic snobbery based on a stupid assumption like Malcolm isn't "qualified" to give an opinion.

  14. #14
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by Elkvis View Post
    I have to agree with Elysia here. HelpfulPerson clearly does not understand C++, and is therefore decidedly unqualified to make statements about its general usefulness. Perhaps it's not useful to HelpfulPerson, but the statement was made in reference to C++ and its usefulness in a general sense. This is made clear by the stated belief that many C++ programs that "ought to be written in C." The simple fact is that a program ought to be written in whatever language the programmer feels comfortable using, that offers the tools necessary to complete the task. HelpfulPerson is obviously ignorant of C++, and nobody is placing any blame for that, and it's hardly the point. A great deal of arrogance was shown by HelpfulPerson by deciding what language programs ought to be written in - programs over which he/she has no control or influence. It actually reminds me a lot of Linus Torvaalds' opinion of C++.
    Those are very pseudo statements about my beliefs/opinions. Let me rephrase my question in a more lucid manner for those of you whom are having trouble interpreting what I think.

    Would THIS CODE be SIMILAR to a CLASS in C++? IF SO THEN what is THE POINT TO HAVING IT IN NEWER LANGUAGES?

    It was merely a conceptual question. I was asking if my code was about the same( similar, but not equal ) to a class in C++( or any other language for that matter ), I did not state I thought that my code was identical. Nor did I state anything about the value of C++ as a language in any manner. As I stated above, I never said I'm a C++ expert, or even have learned one function in that language. I also wasn't making arrogant remarks about how all programs should be written in C. I don't hate C++ at all, in most ways it probably is easy to do things ( such as the WinAPI ) in C++ instead of C. As for your other statements, I think you would see your fault in them if you actually re-read my topic post.

    In summary, you need to look at my actual post instead of trying to judge me based on whatever biases you harbor inside. I wasn't intending to start a flame war. I was merely wanting to know if my code was similar, and if that statement was true, I was asking what classes helped to achieve in newer languages.
    Last edited by HelpfulPerson; 08-08-2013 at 07:27 AM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is similar, but note as pointed out earlier, that higher level languages make it easier to use this feature, enables more optimizations and builds upon it in ways C programmers have to use a lot of code to replicate. How do you simulate inheritance in C? It's possible, but it's tricky to get right.
    Also note how you store function pointers in the struct. C++ does not, so it actually saves memory (unless you use virtual functions).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I dont understand why this wouldn't work
    By We'reGeeks in forum C Programming
    Replies: 28
    Last Post: 03-02-2011, 03:26 AM
  2. I've a problem. Obviously, else I wouldn't be here.
    By Yinyang107 in forum C Programming
    Replies: 6
    Last Post: 12-11-2010, 09:56 AM
  3. please help i'm so new to this you wouldn't believe
    By jaquenta in forum C Programming
    Replies: 7
    Last Post: 12-09-2008, 08:49 AM
  4. Why wouldn't it let me post?
    By deathstryke in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-10-2002, 04:50 AM
  5. wouldn't it be funny if...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 02-05-2002, 06:11 PM