小编Pax*_*337的帖子

Accessing struct via pointer in C and C++

"Simple" question put short:

Why exactly does

JNIEnv *g_env = NULL;
(*g_env)->ExceptionDescribe(g_env);
Run Code Online (Sandbox Code Playgroud)

compile in gcc (C)

but not in g++ (C++)

error: base operand of ‘->’ has non-pointer type ‘JNIEnv’ {aka ‘JNIEnv_’}
Run Code Online (Sandbox Code Playgroud)

As I am working mainly with C++ I don't see why it should compile. As stated by the error, dereferencing the pointer will yield a "variable" and not a pointer anymore. I.e.: in C++ it would be either

g_env->ExceptionDescribe
Run Code Online (Sandbox Code Playgroud)

or

(*g_env).ExceptionDescribe
Run Code Online (Sandbox Code Playgroud)

as its not JNIEnv **

c c++ struct pointers jnienv

2
推荐指数
1
解决办法
106
查看次数

std :: make_pair,std :: unordered_map以及键类型中的move构造函数的用法

给出以下结构:

struct FieldNo
{
    FieldNo() : a('0'), b('0') {}
    FieldNo(char a_, char b_) : a(a_), b(_b) {}
    // copy construction and assigment not allowed
    FieldNo(const FieldNo& other) = delete;
    FieldNo& operator=(const FieldNo& other) = delete;
    // move construction and assignment ok
    FieldNo(FieldNo&& other) = default;
    FieldNo& operator=(FieldNo&& other) = default; 

    char a;
    char b;
};

enum class Members : int8_t
{
    FOO,
    BAR
};
Run Code Online (Sandbox Code Playgroud)

我正在FieldNo用作Memberstd :: unordered_map的键和值。省略哈希创建函数的代码,我的地图定义如下:

typedef std::unordered_map<FieldNo, Members, FieldNoHasher> MyMapT;
Run Code Online (Sandbox Code Playgroud)

后来我用下面的方法初始化并返回到调用者

const MyMapT& map() …
Run Code Online (Sandbox Code Playgroud)

c++ move

1
推荐指数
1
解决办法
63
查看次数

标签 统计

c++ ×2

c ×1

jnienv ×1

move ×1

pointers ×1

struct ×1