CLI通过函数将C++自定义变量赋值给CLI自定义变量

// CLI-FunctionParameterPass.cpp : main project file.

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

#pragma managed
#include <msclr\marshal_cppstd.h>

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections;
using namespace msclr::interop;

public value struct Node
{
  String^ m_Name;
  int^ m_Number;
};

class CNode {
public:
  std::string m_Name;
  int m_Number;
};


void GetNodeCollection(List<Node>^ nodeCollection, std::vector<CNode>& cnodeCollection)
{
  for each (CNode var in cnodeCollection)
  {
    Node node;
    node.m_Name = marshal_as<String^>(var.m_Name);
    node.m_Number = var.m_Number;
    nodeCollection->Add(node);
  }
}

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

  CNode cnode1;
  cnode1.m_Name = "name1";
  cnode1.m_Number = 11;
  CNode cnode2;
  cnode2.m_Name = "name2";
  cnode2.m_Number = 22;
  std::vector<CNode> cnodeCollection;
  cnodeCollection.push_back(cnode1);
  cnodeCollection.push_back(cnode2);
  for each (CNode var in cnodeCollection)
  {
    std::cout << "CNode m_Name:" << var.m_Name << std::endl;
    std::cout << "CNode m_Number:" << var.m_Number << std::endl;
  }

  List<Node>^ nodeCollection = gcnew List<Node>(cnodeCollection.size());
  GetNodeCollection(nodeCollection, cnodeCollection);

  for each (Node var in nodeCollection)
  {
    Console::WriteLine("Node m_Name:{0}", var.m_Name);
    Console::WriteLine("Node m_Number:{0}", var.m_Number);
  }

  system("pause");

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值