gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > This is a C++ question

#72875 - LOst? - Wed Feb 22, 2006 7:22 am

Again, I have a question, and I feel I will soon get replies telling me to buy a C++ book.

Well, I just got Visual C++ 2005, and Microsoft's .NET Framework 2. In the framework, a class named Int32 exists. However, I don't know how it works. This is what it can do:

Code:

int this_is_a_normal_int = 5;

String* string = this_is_a_normal_int.ToString();


The prototype looks something like this (it is part of the Int32 class I think):
public: virtual String* ToString();

This is very interesting. I would like to build a class myself that can override a normal integer like Microsoft did. How do I do? I don't want to cast!
Maybe it is easy? I have never tried. Just give me a little hint and I will be happy.

What I like about this thing is that I may be able to do string conversations, fixed float types and stuff like that for my DS games. I really would love to use this type of things. I think it is very interesting!
_________________
Exceptions are fun

#72878 - sajiimori - Wed Feb 22, 2006 7:57 am

You can't make that work in C++ if you declare the variable as int, but it will work if you declare it as your own custom type and make the constructor take an int.

And yes, buy a C++ book.

Edit: BTW, your signature makes you sound ignorant of one of the most important concepts in all of computer science. It also uses "thine" incorrectly.

#72880 - NoMis - Wed Feb 22, 2006 8:53 am

Int32 is a framework type. It is not possible to use it on the DS because the framework does not run on the DS. You can only use it with Managed C++ wich only runs on windows.

NoMis
_________________
www.gamedev.at - The austrian gamedev site
hde.gamedev.at - The Handheld Dev Env plugins for Eclipse

#72881 - keldon - Wed Feb 22, 2006 9:03 am

sajiimori wrote:
Edit: BTW, your signature makes you sound ignorant of one of the most important concepts in all of computer science. It also uses "thine" incorrectly.


Yes, this was mentioned before in the collision detection thread. But he says he knows about, and uses recursion.

#72988 - sajiimori - Wed Feb 22, 2006 8:03 pm

The fact that more than one person has reached the same conclusion only supports my point. :)

NoMis, while it's not possible to use the actual Int32 from the .NET framework without porting the framework, it's definitely possible to define a standard C++ type with many of the same capabilities.

#72993 - keldon - Wed Feb 22, 2006 8:18 pm

sajiimori wrote:
The fact that more than one person has reached the same conclusion only supports my point. :)

Yes. I was thinking of commenting on it until he acknowledged recursion, and said that he uses it.

#73082 - LOst? - Thu Feb 23, 2006 8:25 am

sajiimori wrote:
You can't make that work in C++ if you declare the variable as int, but it will work if you declare it as your own custom type and make the constructor take an int.


But it worked when I declared the variable as int. That's why I want to know how it is possible. Maybe the header files will help me then.

And I will buy a C++ book.


EDIT:
Maybe it is operator int() I am trying to do?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_conversion_functions.asp
_________________
Exceptions are fun

#73096 - keldon - Thu Feb 23, 2006 9:45 am

.NET is completely object oriented; and visual c++ supports .NET so they must have provided support for these object types. All primitives are objects with .NET.

#73099 - gauauu - Thu Feb 23, 2006 9:58 am

Could be nasty preprocessor tricks, so what you think is an int really isn't.

For example, I wrote and ran this program:

Code:

#include <cstdlib>
#include <iostream>
#include "cheating.h"

using namespace std;

main()
{
   int x = 4;
   cout << x.doVoodoo() << endl;
   return 0;
   
}


and it compiled without eror, and displayed the number 23.

How?

Oh, my cheating.h looked like this:
Code:

class myint
{
   private:
      int val;
   
   public:
      myint(int x);
      int operator=(int x);
      int doVoodoo();

};

myint::myint(int x)
{
   val = x;
}

int myint::operator=(int x)
{
   val = x;
   return val;
}

int myint::doVoodoo()
{
   return 23;
}

#define int myint


One thing I've learned in life is that the preprocessor is a wonderful and dangerous thing.

#73115 - strager - Thu Feb 23, 2006 1:56 pm

Using the preprocessor to override the int is a good idea, but you must be careful with it. For example, you must be able to convert it to other types (such as a plain int and a float).
A "ToString" function doesn't sound right to me; why not just have:
Code:
cout << (String &)myint;

As opposed to:
Code:
cout << myint.ToString();

I think the first looks better (and makes the int look like a real int), yet the second is more understandable (but confusing when a user looks at the type "int"). Whever one you choose, if either, is up to you. With the example given in gauauu's post, and with the power of changing how the compiler typecasts, both are possible, and can be interchangable. Also, if both are implemented, one could give other programmers of the project the power to choose the style they like best. Don't worry about bloating your code; the (String &) typecast could return the results of the int::ToString() function, and vice versa.

#73122 - keldon - Thu Feb 23, 2006 2:55 pm

I'm pretty positive that's a .NET type function. Note that the function started with a capital letter - which is what they do in .NET.

Like I said, in .NET even the primitives are objects.

#73140 - tepples - Thu Feb 23, 2006 5:26 pm

strager wrote:
A "ToString" function doesn't sound right to me

You haven't done any Java programming, right? The .NET framework and C# language were heavily inspired by the Java platform and language.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#73191 - sajiimori - Thu Feb 23, 2006 9:30 pm

C# is not C++. Managed C++ is not C++. If you declare the variable as int in C++, you can't call methods on it, period.

Quote:
Using the preprocessor to override the int is a good idea...
Wow. No, but it might get you on The Daily WTF.

#73205 - strager - Thu Feb 23, 2006 10:21 pm

tepples wrote:
strager wrote:
A "ToString" function doesn't sound right to me

You haven't done any Java programming, right? The .NET framework and C# language were heavily inspired by the Java platform and language.

I don't really like Java. The "ToString" function scares me. Java uses too many classes. The "ToString" function scares me. And, uh, Java is a platform? Hmm...
I realize that C# is somewhat greety in that it takes from Java, but that's just Microsoft doing their job. But, with the VS.NET IDE and such, C# is much easier to get working than Java. The major downside is that C# is not very portable, while Java is.

#73210 - tepples - Thu Feb 23, 2006 10:23 pm

strager wrote:
The major downside is that C# is not very portable

As the Mono CLR gets ported to more platforms, it may very well catch up with Java.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#73226 - keldon - Fri Feb 24, 2006 12:10 am

strager wrote:
tepples wrote:
strager wrote:
A "ToString" function doesn't sound right to me

You haven't done any Java programming, right? The .NET framework and C# language were heavily inspired by the Java platform and language.

I don't really like Java. The "ToString" function scares me. Java uses too many classes. The "ToString" function scares me. And, uh, Java is a platform? Hmm...
I realize that C# is somewhat greety in that it takes from Java, but that's just Microsoft doing their job. But, with the VS.NET IDE and such, C# is much easier to get working than Java. The major downside is that C# is not very portable, while Java is.


If you didn't know; java and .NET runs in a virtual machine. Your java and .NET classes compile into bytecode, which is then interpreted by the JVM (for java) and the CLI - I believe (for .NET). However .NET uses a fast Jitter and outperforms java, and in some cases [they claim] can outperform machine dependant compiled C++. This is probably down to the cache optimisations which can be made. Just remember Microsoft's agreement with Intel - and the amount of code which they have to test cache optimisation with.

Just thought I'd mention that.

#73420 - byg - Sat Feb 25, 2006 5:20 pm

strager wrote:
The "ToString" function scares me.

But why? It's just a method call on an object. Like any other method call. In the case of primitive data types one just has to realise that they are first cast into wrapper classes.

keldon wrote:
However .NET uses a fast Jitter and outperforms java...

I'm not sure what a Jitter is (a Just-In-Time compiler?), but surely this is too generic and there are a host of things to consider before one can say that programming language A outperforms programming language B. I am sure Sun probably have some research which shows the opposite is true!
_________________
Modasi Games
www.modasi.com

#73423 - keldon - Sat Feb 25, 2006 5:39 pm

byg wrote:
strager wrote:
The "ToString" function scares me.

But why? It's just a method call on an object. Like any other method call. In the case of primitive data types one just has to realise that they are first cast into wrapper classes.

keldon wrote:
However .NET uses a fast Jitter and outperforms java...

I'm not sure what a Jitter is (a Just-In-Time compiler?), but surely this is too generic and there are a host of things to consider before one can say that programming language A outperforms programming language B. I am sure Sun probably have some research which shows the opposite is true!


.NET performs on average 5% slower than compiled c++; and can outperform c++ in some [very few] applications. It's just accepted fact - java is way slower than .NET hands down.

Java is very very slow in fact. Even simple loops using only integers run visibly slower. There is Jitting with java; but I'm not sure if it is used all the time. .NET will always Jit commonly executed code using a caching system.

#73427 - byg - Sat Feb 25, 2006 6:02 pm

keldon wrote:
.NET performs on average 5% slower than compiled c++; and can outperform c++ in some [very few] applications. It's just accepted fact - java is way slower than .NET hands down.

Java is very very slow in fact. Even simple loops using only integers run visibly slower. There is Jitting with java; but I'm not sure if it is used all the time. .NET will always Jit commonly executed code using a caching system.


I'm not advocating one over the other. You may be right about the speed comparison but this is becoming less and less of an issue (also surely the VM implementation will have a say in this). Even today some time critical code is implemented in asm rather than C or C++ (wasn't C++ derided at one stage for being slower than C?). One could even write C code that is slower than Basic if one didn't know what they were doing. Comparing simple loops using only integers may be slower on your machine but is not really a real world test. Also, try running the C# test on a Linux machine under Mono. When you previously said outperforms there are many factors to consider (i.e. development speed, ease of debugging, target device etc etc).
_________________
Modasi Games
www.modasi.com

#73436 - keldon - Sat Feb 25, 2006 6:41 pm

Actually I take that back. Java's Jitter does the same thing the .NET one does - and reports say the same things about it. I was just checking out osnews.

But yet one of the developers for sodarace told me that implementing it in java forced them to do clever optimisations for the physics because of java's speed. Same thing I was told about implementing my texture mapping algo in java. I guess it must just be with those types of algorithms.

#73467 - sajiimori - Sat Feb 25, 2006 9:53 pm

What you are comparing is the relative speed of implementations of Java and C#, not the relative speed of the languages.

#73468 - keldon - Sat Feb 25, 2006 10:14 pm

I am talking about the speed of running the same code using the .NET CLI and JRE; not language speed.

Apparently they are supposed to be in the same league. Studies show that they can both outperform compiled code in some cases down to cache optimisation not available to conventionally compiled code and the fact that the JIT compiler will compile code specifically for the processor being ran on.

One thing I just realised is that I have never compiled my code with optimisations on. Let me go do that now and see some speed improvements.

The osnews 9 language benchmark