0

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;
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • I guess Im asking how to handle my "no pointers" situation in C# – user3884747 Feb 23 '18 at 18:25
  • Hi Ken, I took great care in asking these questions, I did not simply dump code and expect an answer. If you read my C# section you will notice the conversion so far with a knowledge gap on how to handle pointer inside C#. Im simple trying to gain understanding how this should be handled and some addition confirmation on my code. – user3884747 Feb 23 '18 at 18:33
  • If the problem is multiple questions I will be happy to split them up into multiple threads. – user3884747 Feb 23 '18 at 18:37
  • 1
    @user3884747 asking **one question** after pushing the [Ask Question] button will likely provide better results for you. I don't think that anyone is questioning whether you took great care. The message is that this is not an appropriate post for this site, for reasons already stated. – Drew Dormann Feb 23 '18 at 18:40
  • Edited to one question – user3884747 Feb 23 '18 at 18:47
  • @user3884747 • To do what you are doing requires familiarity with C++ and C#. What is idiomatic in C++ may be awkward in C#, and vice versa. So a direct 1-to-1 porting will be challenging and disappointing. You will need to do a semantic porting from C++ to C#, and adapt to C# idioms. All of which is beyond the scope of this Q&A forum. I recommend C# 6.0 and .NET 4.6 Framework book by Troelsen & Japikse. – Eljay Feb 23 '18 at 18:48
  • Thanks for the edit. I've retracted my close vote. :-) – Ken White Feb 23 '18 at 18:51

1 Answers1

-1

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.

Jorge Y.
  • 1,123
  • 1
  • 9
  • 16
  • Thanks so much Jorge , Ill add the other questions back in so other can read it, Thanks again I really appreciate it. – user3884747 Feb 23 '18 at 18:54
  • Well, Drew edited my answer because you were going to split the questions. So now the last 4 are just in the edition history. If you split the question (as you should by the guidelines stated by stackoverflow) I would put the other answers in the new questions. If you let the question as it is, then you can still see the other answers in the edition history. – Jorge Y. Feb 23 '18 at 19:03
  • Sounds good removing other questions, sorry for the confusion Im a noob on there – user3884747 Feb 23 '18 at 19:04
  • They won't point to the same object. HexTri is a struct so it will be a copy. – pinkfloydx33 Feb 24 '18 at 00:01
  • You are right, thanks for pointing it out, I have edited the answer to address it. Never noticed that it was a struct in the question sample code, I barely use structs in day to day work, so when I was reading the question I must have skipped the struct declaration assuming it was a class – Jorge Y. Feb 24 '18 at 09:54
  • 1
    Been there ! good comments here.. I can add: If you want to keep some "struct" in for convenience or speed, e.g. for vector types and color pixels, you can pass them as ref-arguments to functions. A struct will not be copied when you use ref. The other things said above are right.. if you want store relations between objects, or maintain lists of objects.. better declare your object as "class" instead of "struct", – Goodies Feb 24 '18 at 10:25