简体   繁体   English

C ++ / C项目中的Getter和Setter?

[英]Getter & Setter in C++/C project?

I am trying to update C/C++ code, want to use getters and setters for tracking and/or testing.我正在尝试更新 C/C++ 代码,想要使用 getter 和 setter 进行跟踪和/或测试。

Made a template class for general declarations为一般声明制作了模板 class

#include <iostream>

using namespace std;

#define TRACK_PROP(a,b) TrackProp<a> b(#b);
#define TRACK_PROP_SET(a,b,c) TrackProp<a> b(#b, c);

template <class T>
class TrackProp {
public:
    T _content;
    const char* Name;

    TrackProp(const char* name) { Name = name; }
    TrackProp(const char* name, T val) { Name = name; _content = val; }
    T operator =(T right)
    {
        if (_content != right) {
            cout << Name << ":" << _content << "->" << right << endl;
            _content = right;
        }
        return _content;
    }

    operator T() const
    {
        return _content;
    }

    T operator ++()
    {
        operator = (_content + 1);
        return _content;
    }
    T operator ++(int)
    {
        operator = (_content + 1);
        return _content - 1;
    }
    T operator --()
    {
        operator = (_content - 1);
        return _content;
    }
    T operator --(int)
    {
        operator = (_content - 1);
        return _content + 1;
    }
    T operator +=(int right)
    {
        operator = (_content + right);
        return _content;
    }
    T operator -=(int right)
    {
        operator = (_content - right);
        return _content;
    }
};

I can easily translate declarations now我现在可以轻松翻译声明

int foo;
int bar = 2;

// These 2 or even some object can be declared by
TRACK_PROP(int, foo);
TRACK_PROP_SET(int, bar, 2);
foo = 1; // = operator /setter
int baz;
baz = bar; // () operator /getter

But having problems when they are used in C files too.但是在 C 文件中使用它们时也会出现问题。

How would you change it to keep Get/Set/Name functionality and be able to use it also in C files as simply as in C++?您将如何更改它以保留 Get/Set/Name 功能并能够在 C 文件中像在 C++ 中一样简单地使用它?

C does not have operator overloading, and C operators are not valid macro names, so there is no way to automagically convert any C operation into a function call. C does not have operator overloading, and C operators are not valid macro names, so there is no way to automagically convert any C operation into a function call. You can certainly implement getters and setters (as top-level functions, because C does not have methods / member functions, either), but they would need to be explicitly declared and explicitly called.您当然可以实现 getter 和 setter(作为顶级函数,因为 C 也没有方法/成员函数),但它们需要显式声明和显式调用。

As far as I understand what you are trying to achieve, then, it's not going to work at all in C.据我了解,您要实现的目标在 C 中根本行不通。 You would need to manually instrument your C sources to get something similar to what your template is giving you on that side.您需要手动检测您的 C 源以获得与您的模板在该方面为您提供的类似的东西。

Addendum附录

There seems to be some confusion about the significance of having both C and C++ code in the same project.在同一个项目中同时拥有 C 和 C++ 代码的意义似乎有些混淆。 In particular, there seems to be an idea that it has significance.特别是,似乎有一种观念认为它具有重要意义。 It doesn't, at least none that applies to the question.它没有,至少没有一个适用于这个问题。

C source files are compiled by a C compiler, and that defines the language features available to those translation units. C 源文件由 C 编译器编译,它定义了这些翻译单元可用的语言特性。 C++ sources are compiled by a C++ compiler, and that defines the language features available within those translation units. C++ 源代码由 C++ 编译器编译,并定义了这些翻译单元中可用的语言特性。 Functions on one side can call functions on the other side, with some caveats, but that's not dependent on belonging to the same project.一侧的函数可以调用另一侧的函数,但有一些注意事项,但这并不取决于是否属于同一个项目。 And it's not sufficient for getting your auto getter/setter trick to apply to C code.让您的自动获取器/设置器技巧应用于 C 代码还不够。

I am a not expert, but isn't it too complicated to generate C++/C functions and replace your variable by some macro?我不是专家,但是生成 C++/C 函数并用一些宏替换变量不是太复杂吗? Your a bit modified version:您稍作修改的版本:

#ifdef __cplusplus
#define TRACK_C(a, b) \
extern "C" a _get$##b() { \
return b; \
}; \
extern "C" void _set$##b(a c) { \
##b = c; \
};
#define TRACK_PROP(a,b) TrackProp<a> b(#b); \
TRACK_C(a, b)
#define TRACK_PROP_SET(a,b,c) TrackProp<a> b(#b, c); \
TRACK_C(a, b)
template <class T>
class TrackProp {
public:
    T _content;
    const char* Name;

    TrackProp(const char* name) { Name = name; }
    TrackProp(const char* name, T val) { Name = name; _content = val; }
    T operator =(T right)
    {
        if (_content != right) {
            cout << Name << ":" << _content << "->" << right << endl;
            _content = right;
        }
        return _content;
    }

    operator T() const
    {
        return _content;
    }
};
#else
#define TRACK_C(a,b) \
a _get$##b(); \
void _set$##b(a);
#define TRACK_GET(b) _get$##b()
#define TRACK_SET(b,c) _set$##b(c)
#endif

In C++ you can still use you operators在 C++ 你仍然可以使用你的运营商

#include <iostream>
#include "TRACK_PROP.h"

using namespace std;

TRACK_PROP(int, demo);

extern "C" void test();

void main(void) {
    demo = 123;
    cout << demo;
    test();
}

And in C replace a demo variable use by my macros:在 C 中替换我的宏使用的演示变量:

#include "TRACK_PROP.h"

TRACK_C(int, demo)

void test() {
    // demo = 1
    TRACK_SET(demo, 1);
    // demo = 3*demo;
    TRACK_SET(demo, 3*TRACK_GET(demo)));
}

It would be great if there will be easier option, but I do not know about any direct way too...如果有更简单的选择会很棒,但我也不知道任何直接的方法......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM