简体   繁体   中英

C++ into C# pointers to a struct inside a struct of same type

I'm trying to convert some C++ into C# for a personal learning project.

Below you will see my C++ and then my attempt at converting it into C# using Unity classes like Vector3.

My question:

How do I handle pointers to a struct inside a struct of the same type?

As far as I can tell it's not possible (HexTri m_nbAB, m_nbBC, m_nbCA;)

Do I need to use a class instead ?

.h file

struct HexTri;


struct HexTile
{
    HexTile( Imath::V3f p );    

    Imath::V3f m_vertPos;       
    Imath::V3f m_nrm;
    enum {
        Terrain_WATER,
        Terrain_DESERT,
        Terrain_GRASSLAND,
        Terrain_FOREST,
        Terrain_MOUNTAIN
    };
    int m_terrain;
    std::vector<HexTri*> m_hextri;  
};

struct HexTri
{
    HexTri( size_t a, size_t b, size_t c );

    size_t m_hexA, m_hexB, m_hexC;
    HexTri *m_nbAB, *m_nbBC, *m_nbCA;
    union {
        size_t m_newvert;
        float m_angle;
    } m_tmp;
};

.cpp file

HexTile::HexTile( Imath::V3f p ) :
    m_vertPos( p )
{
    m_terrain = HexTile::Terrain_DESERT;
    m_nrm = p.normalize();
}

HexTri::HexTri( size_t a, size_t b, size_t c) :
    m_hexA( a ), m_hexB( b ), m_hexC( c )
{
    m_nbAB = NULL;
    m_nbBC = NULL;
    m_nbCA = NULL;
}

Here is my C# conversion so far

using System.Collections;  
using System.Collections.Generic;
using UnityEngine;

public struct HexTile
{
    private Vector3 _position;
    public Vector3 Position        
    {
        get
        {
            return _position;
        }
        set
        {
            _position = value;
        }
    }
    private Vector3 _normal;
    public Vector3 Normal        
    {
        get
        {
            return _normal;
        }
        set
        {
            _normal = value;
        }
    }
    enum terrain{
        Terrain_WATER,
        Terrain_DESERT,
        Terrain_GRASSLAND,
        Terrain_FOREST,
        Terrain_MOUNTAIN
    };
    List<HexTri> hextri;

    public HexTile( Vector3 position, Vector3 normal)
    {
        // Defaults 
        _position =  new Vector3(0,0,0); 
        _normal =  new Vector3(0,0,0);
        hextri = new List<HexTri>();

        // Initilize with value
        Position = position;
        Normal = normal;
    }
}

public struct HexTri
{    

    private int _hexA;   
    public int HexA        
    {
        get
        {
            return _hexA;
        }
        set
        {
            _hexA = value;
        }
    }
    private int _hexB;  
    public int HexB        
    {
        get
        {
            return _hexB;
        }
        set
        {
            _hexB = value;
        }
    }
    private int _hexC;    
    public int HexC        
    {
        get
        {
            return _hexC;
        }
        set
        {
            _hexC = value;
        }
    }

    // Q1 No pointers, cant do this
    HexTri m_nbAB, m_nbBC, m_nbCA;  //?? 
    public HexTri( int a, int b, int c) 
    {
        // Defaults 
        _hexA = -1;//??
        _hexB = -1;//??
        _hexC = -1;//??

        // Initilize with value
        HexA = a;
        HexB = b;
        HexC = c;
    }
}

My take on this:

You are in the right way declaring 3 objects of HexTri type.

EDIT to address the point explained by pinkfloydx33 in a comment: If you declared HexTri as a class instead of a struct, this C# this code makes a and b "point" to the same object:

HexTri a = new HexTri();
HexTri b = a;

A good explanation of the differences between classes and structs in C# (they are not the same differences as in class vs struct in C++) in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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