如何在 C++/CLI 中获取指针的值?
How to get the value of a pointer in C++/CLI?
原生C++ cpp文件:
#include "stdafx.h"
#include <iostream>
int strcpyTest(int dest, int *sour)
{
int s = dest + 10;
std::cout << "s" << s << std::endl;
std::cout << "&s" << &s << std::endl;
sour = &s;
int x = *sour + 20;
std::cout << "sour" << sour << std::endl;
std::cout << "*sour" << *sour << std::endl;
return x;
}
C++/CLI h 文件:
#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
namespace SubFunction {
public ref class Test
{
public:
int StrTest(int d);
int xxx;
};
}
C++/CLI cpp 文件:
#include "stdafx.h"
#include "CPlusPlus.h"
#include "SubFunction.h"
int SubFunction::Test::StrTest(int d)
{
int x;
int dd = strcpyTest(d, &x);
xxx = x;
return dd;
}
C# cs 文件:
int a = 15;
Test ts = new Test();
int x = ts.StrTest(a);
int y = ts.xxx;
MessageBox.Show(x.ToString());
MessageBox.Show(y.ToString());
在最后的MessageBox中,"xxx"是一个指针地址。 "xxx" 第一次有了指针地址值。如果再计算,总是出现0,为什么呢?我不明白。如何获得价值?或者获取"sour"?
的值
尝试设置 *sour = s;
然后你会看到 'x' 的值在 'StrTest()' 中发生了变化,你会得到你期望的 'x' 的行为将具有 's'.
的值
当你设置 sour = &s;
就像你在这里的例子一样,你正在改变本地指针 'sour' 指向的地址,并且 'StrTest()' 无法知道它,因为它是您传入的指针的本地副本。
通过使用“*sour = s”,您正在更改它指向的变量值 'x'。
你可以这样想,本地指针 'sour' 是一个本地副本,它被构建并且只能被 'strcpyTest()' 访问,当它超出范围时被销毁,但是它包含您传入的 'x' 的引用,这样如果您解除对本地指针 'sour' 的引用,您可以修改 x.
的值
插图:
里面
int strcpyTest(int dest, int *sour)
注意:这不是有效语法,仅用于说明目的。
sour ----> [&x] // 包含'StrTest()'
传入的变量'x'的地址
*sour ----> [x] // 获取变量 'x' 的值(取消引用 'sour' 以访问 'x' 的值)
sour = (Address) // Sets a new address to 'sour', which means it no longer points to '&x' that you passed in from 'StrTest()'
*sour = (Value) // Still points to '&x' from 'StrTest()', and assigns a new value to it
无论您传入 '&x' 还是构造一个指针并将其传递给函数 'strcpyTest()',您都会发现 'sour' 将是该指针的本地副本。
旁注:如果你有一个不小的数据结构,那么我建议你做的是 return 从 strcpyTest() 指向它的指针而不是 returning 实际值这样您就可以避免不必要地复制数据,除此之外设置 *sour = s;
.
完全没问题
原生C++ cpp文件:
#include "stdafx.h"
#include <iostream>
int strcpyTest(int dest, int *sour)
{
int s = dest + 10;
std::cout << "s" << s << std::endl;
std::cout << "&s" << &s << std::endl;
sour = &s;
int x = *sour + 20;
std::cout << "sour" << sour << std::endl;
std::cout << "*sour" << *sour << std::endl;
return x;
}
C++/CLI h 文件:
#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
namespace SubFunction {
public ref class Test
{
public:
int StrTest(int d);
int xxx;
};
}
C++/CLI cpp 文件:
#include "stdafx.h"
#include "CPlusPlus.h"
#include "SubFunction.h"
int SubFunction::Test::StrTest(int d)
{
int x;
int dd = strcpyTest(d, &x);
xxx = x;
return dd;
}
C# cs 文件:
int a = 15;
Test ts = new Test();
int x = ts.StrTest(a);
int y = ts.xxx;
MessageBox.Show(x.ToString());
MessageBox.Show(y.ToString());
在最后的MessageBox中,"xxx"是一个指针地址。 "xxx" 第一次有了指针地址值。如果再计算,总是出现0,为什么呢?我不明白。如何获得价值?或者获取"sour"?
的值尝试设置 *sour = s;
然后你会看到 'x' 的值在 'StrTest()' 中发生了变化,你会得到你期望的 'x' 的行为将具有 's'.
当你设置 sour = &s;
就像你在这里的例子一样,你正在改变本地指针 'sour' 指向的地址,并且 'StrTest()' 无法知道它,因为它是您传入的指针的本地副本。
通过使用“*sour = s”,您正在更改它指向的变量值 'x'。
你可以这样想,本地指针 'sour' 是一个本地副本,它被构建并且只能被 'strcpyTest()' 访问,当它超出范围时被销毁,但是它包含您传入的 'x' 的引用,这样如果您解除对本地指针 'sour' 的引用,您可以修改 x.
的值插图:
里面
int strcpyTest(int dest, int *sour)
注意:这不是有效语法,仅用于说明目的。
sour ----> [&x] // 包含'StrTest()'
传入的变量'x'的地址
*sour ----> [x] // 获取变量 'x' 的值(取消引用 'sour' 以访问 'x' 的值)
sour = (Address) // Sets a new address to 'sour', which means it no longer points to '&x' that you passed in from 'StrTest()'
*sour = (Value) // Still points to '&x' from 'StrTest()', and assigns a new value to it
无论您传入 '&x' 还是构造一个指针并将其传递给函数 'strcpyTest()',您都会发现 'sour' 将是该指针的本地副本。
旁注:如果你有一个不小的数据结构,那么我建议你做的是 return 从 strcpyTest() 指向它的指针而不是 returning 实际值这样您就可以避免不必要地复制数据,除此之外设置 *sour = s;
.