Thread: Yet another OOP question about C

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    Yet another OOP question about C

    Hello,
    I know the topic is famous, but I was wondering, how can you actually create private fields in a C struct? I got the concept of VTable and inheriting methods aswell overriding them, but private variables can be obtained with static keyword in sourcefile. If you have variables in a struct, you can access them as soon as you got a ref to the struct. The functions are easy:
    Code:
    struct classA {
      pointerToFunction *p;
    }
    
    static void* thePrivateFoo() { }
    void* publicFoo() {
       thePrivateFoo(); /* or something like that */
    }
    ...
    
    /*in init code somewhere in the c file */
    classAInstance->p = publicFoo;
    But I was thinking about the variables... How is this achievable?
    I was thinking of a struct with only get/set functions and with no datamembers at all. All vars to be static outside the struct. But this kind of destroys the encapsulation.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Use the opaque pointer idiom.
    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
    Sep 2014
    Posts
    121
    Quote Originally Posted by laserlight View Post
    Use the opaque pointer idiom.
    OK... Look at this. It actually works. But I`d rather hear your opinion about the approach. It is working however.
    Private.h
    Code:
    #ifndef PRIVATE_H
    #define PRIVATE_H
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Private;
    typedef void (*setx)(Private*, int newx);
    typedef int (*getx)(Private*);
    
    struct Private_;
    struct Private {
        Private_ *privateField;
        Private *self;
        setx setX;
        getx getX;
    };
    
    typedef void (*__setx)(Private_*, int);
    typedef int (*__getx)(Private_*);
    
    Private* getInstance();
    #endif // PRIVATE_H
    Private.c
    Code:
    #include "Private.h"
    
    struct Private_ {
        Private_* self;
        int __x;
        __setx privateSetX;
        __getx privateGetX;
    
    };
    
    static inline int _getX(Private_ *self) {
        return self->__x;
    }
    
    static inline void _setX(Private_ *self, int newX) {
        self->__x = newX;
    }
    
    static inline int _getx(Private* self) {
        return self->privateField->privateGetX(self->privateField);
    }
    
    static inline void _setx(Private* self , int newx) {
        self->privateField->privateSetX(self->privateField, newx);
    }
    
    Private* getInstance() {
        Private* p = (Private*) malloc(sizeof(Private));
        p->privateField = (Private_*) malloc(sizeof(Private_));
        p->privateField->__x = 0;
        p->privateField->privateGetX = _getX;
        p->privateField->privateSetX = _setX;
        p->getX = _getx;
        p->setX = _setx;
    
        return p;
    }
    and the main.c
    Code:
    #include "Private.h"
    
    int main(int argc, char** argv) {
        Private* p = getInstance();
        p->setX(p, 100);
        printf("p`s X is %d now\n", p->getX(p));
    }
    Actually you can reach p`s privateField but nothing from inside it. Neat

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    My opinion is that the opaque pointer idiom is a good idiom.

    But identifiers with two initial underscores are reserved by the Standard for the implementation, so don't name anything like that.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    121
    Quote Originally Posted by whiteflags View Post
    My opinion is that the opaque pointer idiom is a good idiom.

    But identifiers with two initial underscores are reserved by the Standard for the implementation, so don't name anything like that.
    Yes. I`ll take a note. However I just sketch that code, just to see if it is working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2014, 05:41 PM
  2. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  3. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  4. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM

Tags for this Thread