Sign in to follow this  
wicked357

The C++ equiv of C# (int)

Recommended Posts

I know int functions the same in C++ and C#, but I noticed a difference when looking at this C# code versus this C++ code, for example I want to create a 1D array of int type, so consider this,
int map[5 * 5] =
{
  ...
};

Now in C# you could do something like this,
int mapWidth = map.GetLength(1);
int mapHeight = map.GetLength(0);

But obviously C++ doesn't offer such a function call in type int, so what would you do in C++ to achieve the same goal?

Share this post


Link to post
Share on other sites
Quote:

But obviously C++ doesn't offer such a function call in type int, so what would you do in C++ to achieve the same goal?

The second code snippet you posted is perfectly valid C# and C++, assuming "map" exists and has the appropriate method. I think you should rephrase your question.

Share this post


Link to post
Share on other sites
No the second wont compile in C++, it gives the must be a class/structure to the left of GetLength compile error. I am looking for the equivalent of the C# in C++.

C# already offers that, but C++ doesn't so what is the equivalent of this, or a function that will serve the same purpose?

Share this post


Link to post
Share on other sites
In C++ an array is just a block of data its not an object. Nowhere in it does it store the size of the array.

You can sometimes use the sizeof() operator on the array, but, this only works if the array is a static size and you aren't using sizeof() on a pointer to the array (or it'll tell you the size of the pointer). Don't rely on this!

You can use things like the vector size in stl to get an array-like structure similar to the one in C#.

But in general, in C++ you need to keep track of how many elements are in an array yourself.

Share this post


Link to post
Share on other sites
It seems what you're really asking about is the difference between C++ arrays and C# arrays, not C++'s int type and C#'s int type.
Quote:
C# already offers that, but C++ doesn't so what is the equivalent of this, or a function that will serve the same purpose?
The answer to your question is that in C++, you would (typically) create an array class that stores (along with the array data itself) the dimensions of the array. (Examples of such a class would be std::vector for 1-d arrays, and boost::multi_array for N-d arrays.)

Share this post


Link to post
Share on other sites
So this step is basically not needed than as I already know the size and I could just declare that early on and instead of map[5 * 5], I see I am following this tutorial in C# it is a 10 part video tutorial but I like to port to learn things and better my skills, this is my first attempt at C# -> C++ I have done some AS3 -> C++, but seems to be easier to follow than some C# stuff. Thanks for your assistance.

Share this post


Link to post
Share on other sites
Quote:

No the second wont compile in C++, it gives the must be a class/structure to the left of GetLength compile error. I am looking for the equivalent of the C# in C++.

The actual snippet is valid syntax, however. With the appropriate context (i.e., appropriate definition of a map object) it will compile. This is why I wanted you to clarify the question.

Also, moving to FB.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
The actual snippet is valid syntax, however. With the appropriate context (i.e., appropriate definition of a map object) it will compile. This is why I wanted you to clarify the question.

He posted the definition of the map variable in the 1st snippet, as an array of ints.

Share this post


Link to post
Share on other sites
Quote:
Original post by XTAL256
He posted the definition of the map variable in the 1st snippet, as an array of ints.
True, but since we're talking about two different code samples in two different languages, I think it's reasonable to interpret them as coming from different contexts. (FWIW, when I read them, I did not immediately make the connection between the map variable referred to in the first example and the map variable referred to in the second.)

Share this post


Link to post
Share on other sites
C++:
//In this case you don't know the size of the array, so you'd use sizeof
char *foobar[] = { "foo", "bar", "123", "abc" };
int foobar_size = sizeof(foobar) / sizeof(foobar[0]);


//Here you decide what the size will be, so there's really no need to do it the way you did in C#. You'll also want to avoid using magic numbers.
const int mapWidth = 5, mapHeight = 5;
int map[mapWidth * mapHeight];



jyk: I've never used C#, but I'll assume that the second code snippet only works on 2-dimensional arrays, whereas the first code snippet initializes a 1-dimensional array, so in a way you're right. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Grantax
C++:
*** Source Snippet Removed ***
*** Source Snippet Removed ***

jyk: I've never used C#, but I'll assume that the second code snippet only works on 2-dimensional arrays, whereas the first code snippet initializes a 1-dimensional array, so in a way you're right. :)


Wouldn't the first one just return the size of the pointer to the array? Don't you want to do sizeof(*foobar) or not declare foobar as a pointer to the array.

Share this post


Link to post
Share on other sites
Quote:
Original post by way2lazy2care
Wouldn't the first one just return the size of the pointer to the array? Don't you want to do sizeof(*foobar) or not declare foobar as a pointer to the array.

It's not a pointer to an array, it's an array of pointers. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Grantax
Quote:
Original post by way2lazy2care
Wouldn't the first one just return the size of the pointer to the array? Don't you want to do sizeof(*foobar) or not declare foobar as a pointer to the array.

It's not a pointer to an array, it's an array of pointers. :)


shwoops. I am but a novice pointer reader :(.

char (*foobar)[] = { "foo", "bar", "123", "abc" }; <that's a pointer to the array right? :-/

Share this post


Link to post
Share on other sites
Quote:
Original post by way2lazy2care
shwoops. I am but a novice pointer reader :(.

char (*foobar)[] = { "foo", "bar", "123", "abc" }; <that's a pointer to the array right? :-/

First of all, you probably meant char *(*foobar)[] (Or else you'd have a pointer to an array of char instead of char*)

Though you can't initialize it like that, because it's the value of the pointer you're initializing:
char *foobar[] = { "a", "b", "c", "d" };
char **pFoobar = foobar; //I can't think of any reason you would use (*pFoobar)[]


Share this post


Link to post
Share on other sites
Quote:
Original post by Grantax
Quote:
Original post by way2lazy2care
shwoops. I am but a novice pointer reader :(.

char (*foobar)[] = { "foo", "bar", "123", "abc" }; <that's a pointer to the array right? :-/

First of all, you probably meant char *(*foobar)[] (Or else you'd have a pointer to an array of char instead of char*)

Though you can't initialize it like that, because it's the value of the pointer you're initializing:
*** Source Snippet Removed ***


I actually didn't even pay any attention to the type or anything after the equals sign. I saw it as SOMETHING (*ptr)[] = SOMETHING ELSE;

Share this post


Link to post
Share on other sites
Quote:
Original post by wicked357
I know int functions the same in C++ and C#, but I noticed a difference when looking at this C# code versus this C++ code, for example I want to create a 1D array of int type, so consider this,
*** Source Snippet Removed ***
Now in C# you could do something like this,
*** Source Snippet Removed ***

But obviously C++ doesn't offer such a function call in type int, so what would you do in C++ to achieve the same goal?

Your C# code won't work as expected with that array. Since that's a 1 dimensional array you'll get an exception out of GetLength when you pass it a dimension greater than 0.

As far as C++ goes, you should look at using std::vector, versus manual memory management. As far as storing dimensions of your map goes, store it separately...

class MapData {
int width;
int height;
std::vector<int> data;
public:
MapData(int width, int height) : width(width), height(height), data(width * height) {}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Grantax
C++:
*** Source Snippet Removed ***
*** Source Snippet Removed ***

jyk: I've never used C#, but I'll assume that the second code snippet only works on 2-dimensional arrays, whereas the first code snippet initializes a 1-dimensional array, so in a way you're right. :)


The first snippet only works when you have direct access to a stack-based array. As soon as you have to deal with the array via a pointer (either it's allocated with new[], or is passed to a function and decays to a pointer) it totally falls apart.

The much preferable solution would be to use a container - std::tr1::array or boost::array for a fixed-size array or std::vector for a dynamic array.

Share this post


Link to post
Share on other sites
As a side note, for those who were wondering, Arrays are objects in C#. Everything derives from the Object class (in C#). In this case, creating an array with Something[] = new Something[#]; literally creates a class object. Well, maybe a struct, but probably a class.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this