多个struct变量的值无意中相同

时间:2014-11-14 14:15:12

标签: c struct

我有一个像Java一样的OOP代码 我已将接口和实现分隔为单独的文件名demo.hdemo.c

demo.h

#ifndef DEMO_H
#define DEMO_H



typedef struct {

    /* 
       This is the variable that will be set by setter method
       and its value will be extracted by getter method. 
       This variable must not be directly accessible by the programmer.
     */
    int num; 


    void (* setNum)(int);  // This function will set the value of variable "num".
    int (* getNum)();      // This function will return the value of variable "num".
} *Demo;  // As objects in java are always called by reference.



Demo newDemo();  // This function will create an instance of class(struct here) Demo and return.
/* This is equivalent to:

       Demo obj = new Demo();

   int java.

   I want my users to create instance of this class(struct here) like this:  

       Demo obj = newDemo();  

   here in this code.
 */


#endif  

实施:
demo.c

#include <stdlib.h>
#include "demo.h"


Demo demo;  /* I have created a global variable so that it is accessible 
               in setNum and getNum functions. */



void setNum(int num) {

    demo->num = num;  // This is where the global demo is accessed.
}



int getNum(Demo obj) {

    return demo->num;  // This is where the global demo is accessed.
}



Demo newDemo() {

    Demo obj; // This will be the returned object.


    obj = (Demo)malloc(sizeof(*obj));  /* Allocating separate memory to 
                                          obj each time this function is called. */


    /* Setting the function pointer. */
    obj->setNum = setNum; 
    obj->getNum = getNum;

    /* As obj is at different memory location every time this function is called,
       I am assigning that new location the the global demo variable. So that each variable
       of the Demo class(struct here) must have a different object at different memory
       location. */
    demo = obj;

    return obj;  // Finally returning the object.
}   

这就是我实现main功能的方式:

的main.c

#include "demo.h"
#include <stdio.h>



int main() {

    void displayData(Demo);


    Demo obj1 = newDemo();
    Demo obj2 = newDemo();
    Demo obj3 = newDemo();


    obj1->setNum(5);
    obj2->setNum(4);
    obj3->setNum(12);



    displayData(obj1);
    displayData(obj2);
    displayData(obj3);



    return 0;
}



void displayData(Demo obj) {

    int num = obj->getNum();

    fprintf(stdout, "%d\n", num);
}  

关于我的mac book pro的编译和执行:

> gcc -c demo.c
> gcc main.c demo.o -o Demo
> ./Demo  

输出结果为:

12
12
12

但是所需的输出是:

5
4
12

我做错了什么? 请帮忙。
我不希望我的用户将结构指针作为参数传递:

Demo obj = newDemo();
obj->setName(obj, "Aditya R.Singh"); /* Creating the program this way was successful as my 
                                        header file had the declaration as: 

                                            typedef struct demo {

                                               int num;
                                               void (* setNum)(struct demo, int); // This is what I don't desire.
                                               void (* getNum)(struct demo); // This is what I don't desire.
                                            } *Demo;    

                                        I want to keep it like the way it is in my current
                                        demo.h*/  

/* I don't want to pass obj as an argument. All I want to do this is this way. */
obj->setName("Aditya R.Singh");

有没有办法做到这一点并获得所需的输出?

请帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我完全不知道c++,但在您的代码中,我认为demo = obj;是问题所在。 demo是全球性的,对吧?通过对newDemo()的轻快调用,它将被覆盖。

副作用:内存泄漏。