CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2008
    Posts
    13

    Convert this line from C++ to C#

    Hi!

    #define ROTATE(a,i,j,k,l) g=a[i][j]; h=a[k][l]; a[i][j]=g-s*(h+g*tau); a[k][l]=h+s*(g-h*tau);

    I have this line in C++; I have no idea how to represent it in C#. Is taht some kind of inline function? How that define work? I usually use #define to define some constant to use with if... this code... else... this code, but here it seems like performing some operations. Also variable "tau" seems to come out of nowhere, or that is some C++ constant?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Convert this line from C++ to C#

    First of all, look in your existing source code to find out where ROTATE(...) is used. You should find that the variables g and tau are either constants, globals or locals that are already defined.

    So once you understand what's really going on, you can write an equivalent method in C# that does what you want. The method could be static depending on whether you pass in all the data required or if some of the data is from a class field.

  3. #3
    Join Date
    Jul 2008
    Posts
    13

    Re: Convert this line from C++ to C#

    Thank you, so I assume it is just safe to convert it to actual function? Wonder why use #define in the first place at all, but I'll just make static function from it then.

  4. #4
    Join Date
    Jul 2008
    Posts
    13

    Re: Convert this line from C++ to C#

    I have another problem, need to understand how that line of code works:
    function calling in C++:
    split_tris(&m->tris[first_tri], num_tris, axis, coord);

    and actual function is...

    split_tris(Tri *tris, int num_tris, PQP_REAL a[3], PQP_REAL c)
    {
    .....
    }

    What is purpose of &m->tris[first_tri]?
    Like, function expect to get pointer with triangle struct array, what it actually gets when we sent &m->tris[first_tri] instead of expected pointer?

    What is "&" mark for and how it work when insted of pointer we pass [first_tri]?

    I just don't get that function because of this. For example, it never gets value 4900, though inside of that function, tris[4900] is set. I think taht is all because of that construct &m->tris[first_tri], but I can't wrap my head around it

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert this line from C++ to C#

    Quote Originally Posted by RyuMaster View Post
    What is purpose of &m->tris[first_tri]?
    Like, function expect to get pointer with triangle struct array, what it actually gets when we sent &m->tris[first_tri] instead of expected pointer?
    All that function calls for is a pointer, and that is exactly what is being passed -- a pointer.
    What is "&" mark for and how it work when insted of pointer we pass [first_tri]?
    So you aren't aware of what the address-of operator is supposed to do?

    If you do not know a basic concept such as passing the address of a variable, then write very simple code to familiarize yourself with such concepts:
    Code:
    void func(int *p)
    {
       *p = 10;
    }
    
    int main()
    {
        int x = 0;
        func(&x);
    }
    What does this code do? Now compare the code you have now with this simple code.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 30th, 2013 at 06:07 AM.

  6. #6
    Join Date
    Jul 2008
    Posts
    13

    Re: Convert this line from C++ to C#

    Quote Originally Posted by Paul McKenzie View Post
    All that function calls for is a pointer, and that is exactly what is being passed -- a pointer.
    So you aren't aware of what the address-of operator is supposed to do?

    If you do not know a basic concept such as passing the address of a variable, then write very simple code to familiarize yourself with such concepts:
    Code:
    void func(int *p)
    {
       *p = 10;
    }
    
    int main()
    {
        int x = 0;
        func(&x);
    }
    What does this code do? Now compare the code you have now with this simple code.

    Regards,

    Paul McKenzie
    Thank you, using "address-of" keyword I was finally able to find what is was about. I'm confused not really about address, but about that part: tris[first_tri]

    In C#, that would mean, that I'm passing not whole Tris[] array, but just some element under number first_tri.

    But in C++ function, I see that it uses Tris[] as whole array inside functions, then why bother passing tris[some_value] at all, and not just *tris? Though, now I'll go read in-depth abour address of operator more, maybe that will give some clue.

    I just debugged, that most likely &tris[first_tri] indeed cuts half of the array away and start irritating from the middle. Seems like that was made for speed gain, oh dat C++
    Last edited by RyuMaster; January 30th, 2013 at 07:27 AM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert this line from C++ to C#

    Quote Originally Posted by RyuMaster View Post
    Thank you, using "address-of" keyword I was finally able to find what is was about. I'm confused not really about address, but about that part: tris[first_tri]
    All the person was doing is passing the address of a single element.

    An array is made up of single elements. So if I have an array of 10 items, and I only want item 3 to be changed, then please look at this code:
    Code:
    void func(int *p)
    {
       *p = 10;
    }
    
    int main()
    {
        int x[10] = {0};
        func(&x[3]);
    }
    When func returns, x[3] will magically be equal to 10. Again, write smaller programs that mimic what you're doing, and it should be quite clear what is being done.

    Secondly, the reason why it seems that the function "cuts off" half the array is that an array consists of contiguous elements. So when a pointer is passed to an element in the array, you can access the other items after it since they follow each other in memory:
    Code:
    void func(int *p)
    {
       *p = 10;
       *(p+1) = 25;
    }
    
    int main()
    {
        int x[10] = {0};
        func(&x[3]);
    }
    After the function is called, x[3] is 10, and x[4] is 25.
    In C#, that would mean, that I'm passing not whole Tris[] array,
    You need to forget about what it would be in C#. In C++, arrays are not really passed, only the address of the start of the array. Yes, you can pass arrays with tricky syntax that I won't go into, but in general, arrays are never passed, only pointers.
    But in C++ function, I see that it uses Tris[] as whole array inside functions,
    Wrong.

    The [] syntax inside that function is just nice looking pointer syntax. The whole array is not passed.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 30th, 2013 at 02:02 PM.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Convert this line from C++ to C#

    Quote Originally Posted by RyuMaster View Post
    Wonder why use #define in the first place at all
    #define is a pre-processor directive. It makes sense to use in C++ because it's code shorthand. Macro use is popular in C++, such as the MFC/ATL TRACE macro.

    The drawback to them in C++ is that you can't step into them while debugging.

    In C#, you can do something similar using a static method. You can't do everything with a static method, because static methods aren't pre-processor directives; however, this is both good and bad because preprocessor directives aren't type safe, but sometimes you need a macro that takes advantage of the preprocessor and a static method won't do.

    On the bright side, in the 10 years I've spent programming in C#, I have yet to run into a situation where I couldn't convert a C++ macro into an equivalent C# method. Sometimes, I needed to refactor the code a bit, but was always able to get it done. In some cases, it might be better to understand the C++ code and rewrite it in a C# way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured