Thread: API in C programming

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    API in C programming

    Hi, I'm just wondering if I want to use function as a CLASS like java language .. must I do a header File? and build inside it a struct with the name function I want, but the question How can I initiate the variable of my built struct? for example in java there's a constructor that I can setting up(initializing) the member of the class by new(), how about C programming how it's going?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you really need to implement some kind of inheritance and polymorphism? If not, the usual approach of having a struct type and a core library of functions that operate on it (including init and cleanup) will do for abstraction and encapsulation: method call syntax is just syntactic sugar. You can have the "data hiding" aspect of encapsulation by using the opaque pointer idiom and thereby ensure that users of your struct type cannot access its members directly, thus allowing you to change the "private internals" of the "class" at will. Likewise, static functions in the source file used to implement the struct's associated functions can serve as "private member functions".
    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
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Do you really need to implement some kind of inheritance and polymorphism? If not, the usual approach of having a struct type and a core library of functions that operate on it (including init and cleanup) will do for abstraction and encapsulation: method call syntax is just syntactic sugar. You can have the "data hiding" aspect of encapsulation by using the opaque pointer idiom and thereby ensure that users of your struct type cannot access its members directly, thus allowing you to change the "private internals" of the "class" at will. Likewise, static functions in the source file used to implement the struct's associated functions can serve as "private member functions".
    I just wished to do implement some kind of inheritance .. thanks anyway !

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is rather strange to have "just wished to do implement some kind of inheritance" when you didn't mention that in post #1: your question was about object construction, not how to do inheritance.

    As for inheritance and polymorphism: the typical approach is to construct a virtual table of function pointers such that you basically have function pointers as data members of the struct, allowing the "derived struct" to replace what these pointers point to.
    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

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    It is rather strange to have "just wished to do implement some kind of inheritance" when you didn't mention that in post #1: your question was about object construction, not how to do inheritance.

    As for inheritance and polymorphism: the typical approach is to construct a virtual table of function pointers such that you basically have function pointers as data members of the struct, allowing the "derived struct" to replace what these pointers point to.
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread