在 /clr 中使用非托管 c++ 对象

Using unmanaged c++ objects in /clr

本文关键字:c++ 对象 clr      更新时间:2023-10-16

我有一些像下面的类这样的类是用 c++ 编写的,我必须将它们实现到 Windows 窗体中。是否有任何解决方案可以在 Windows 窗体/clr 类中创建非托管对象?

#pragma once
#ifndef _HOTEL_H
#define _HOTEL_H
#include "Room.h"
#include "Adress.h"
#include "Employee.h"
#include "Apartament.h"
#include "TechnicalRoom.h"
#include "RecreationRoom.h"
#include <vector>
#include <string>

using namespace std;
class Hotel {
protected:
int HotelID, HotelStars, NumberOfEmployee, NumberOfClients, NumberofRooms;
string HotelName;
Adress HotelAdress;
vector <Room*> Rooms;
vector <Person*> People;
public:
//methods
Hotel(int = 3, string = "Hotel");
~Hotel();
string getName();
int getNumberOfClients();
int getNumberOfEmployee();
int getHotelStars();
void changeNumberOfStars(int);
void BookApartament(int, int);
void AddRoom(int);
void DeleteRoom(int);
void AddEmployee();
void DeleteEmployee(int);
friend ostream & operator<< (ostream &out, Hotel &h);
friend ref class MainWindow;
};
#endif

听起来你可能想要一些类似的东西:

namespace SomeCompany
{
    public ref class Hotel
    {
        ::Hotel* pHotel;
        public:
            Hotel() : pHotel(new ::Hotel()) {}
            ~Hotel() {
               delete pHotel;
               pHotel = nullptr;
            }
            !Hotel() {
               delete pHotel;
            }
            // ... etc. ... 
    };
}

有关更多详细信息,请参见如何:包装本机类以供 C# 使用。