• Welcome to Valhalla Legends Archive.
 

VB vs. C++

Started by MoNksBaNe_Agahnim, November 14, 2003, 06:16 PM

Previous topic - Next topic

MoNksBaNe_Agahnim

I am making this post for those who don't know the differences between VB and C++ and what each has to offer (myself included  ;D). Please no flames about VB sux or vise versa.

Overall what does VB bring that C++ doesn't and vise versa and what are each mainly used for, i.e. lots of games are made in c++ now a days...

Hitmen

Visual Basic is for rapid application development, and has some limitations compared to C++, which can do most anything you want it to.

Banana fanna fo fanna

C++ is designed to bring the benefits of OOP together with giving the client programmer precise, low-level control of the machine. If you ask your average programmer, it is *the* programming language. It isn't perfect by any means; it's easy to write insecure software, cryptic code, and can be slow for developing because it does not safeguard the programmer from stupid flaws.

VB is designed for fast development of GUI based prototypes. It's based on a proven beginner language, so its simple user interface and syntax are good for newbies (I disagree, but this is the common consensus). It has numerous disadvantages, such as reliance on ActiveX technologies, its large executable sizes and their slow execution speed, and its lack for contempory language features (most notable are OOP and exeception handling. VB's error handling sucks, and no one can deny that).

Hitmen

Quote from: St0rm.iD on November 14, 2003, 08:45 PM
VB's error handling sucks, and no one can deny that
VB.NET gets try...catch :P

ObsidianWolf

I love VB but honestly theres somethings that you cant do in VB.net that you can in C++.  Proof of such is the error i sometimes recieve that mocks me "You can not do this in Visual Basic".

MyndFyre

such as.... pointers?  :D
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

CupHead

#6
As much as I hate this thread, I'm going to post some stuff anyway.  First of all, I like VB much, much, much better than C/C++.  Personally, I haven't really come across anything that I "couldn't do" that I wanted to have done.  Secondly, despite the fact that people are always complaining about the lacks of features that VB has, I rarely, if ever, see questions about how to accomplish.  So either people just give up on the idea that VB can do what they want or they never run across these problems that VB can't solve.  Either way, it can be done.  I admit, there are certain things it's certainly not worth the effort to do in VB (inline assembly, for instance), but for the most part, people don't need anything lower level.

Ok, so let's address some of these issues that people frequently complain about.
1) Pointers

Yes, Visual Basic does have pointers.  You can use the functions VarPtr(), StrPtr(), and ObjPtr() to return the address of a variable.  VarPtr works on variables and arrays.  For example, to return the address of the base of an array: ArrayBaseAddress = VarPtr( MyArray(0) )  StrPtr is different from VarPtr because strings in VB are pointers to pointers to strings, so if you use VarPtr on a string, the value you get back is the pointer to the actual contents.  However, if you use StrPtr(SomeString), the value returned will be the address of the first character in the string.  Lastly, we have ObjPtr, which should be pretty easy to figure out.  It just returns the address of an object for whatever reason you might need.  For functions, you use the AddressOf keyword.  For instance, when subclassing and setting your own callback routine, you need to specify the address of the callback function.  This is done by using AddressOf to return the function's location in memory.

2) Function Overloading

Visual Basic does not have any support for overloading functions, however, it can be done in a roundabout way.  Observe:


Public Enum DataTypes
 vbEmpty = 0
 vbNull = 1
 vbInteger = 2
 vbLong = 3
 vbSingle = 4
 vbDouble = 5
 vbCurrency = 6
 vbDate = 7
 vbString = 8
 vbObject = 9
 vbError = 10
 vbBoolean = 11
 vbVariant = 12
 vbDataObject = 13
 vbDecimal = 14
 vbByte = 17
 vbUserDefinedType = 36
 vbArray = 8192
End Enum

Private Function AddToBuffer( Blah as ParamArray )
 Select Case VarType(Blah(0)) 'Check the first parameter's data type
   Case DataTypes.vbEmpty
     Exit Function
   Case DataTypes.vbString
     PacketBuffer.InsertString Blah(1)
   Case DataTypes.vbLong
     PacketBuffer.InsertDWORD Blah(1)
 End Select
End Function


It's crude, but it might as well be an overloaded function.

3) Operator Overloading

Yeah, as far as I know, there's no way to do anything close to this in VB.  Oh well.

4) Exception Handling

VB has excellent error handling in my opinion.  You can not only create your own errors, but you can raise them whenever you like, be notified of what error it is, what line caused it, and even return control to the program after your code has adjusted.  We all know about On Error Resume Next, which is essentially a catch-all that keeps your programs from crashing.  Well, you can actually do:


Private Function MyFunction() as Whatever
FunctionStart:
 On Error Goto ErrorHandler

 MyVar = Mid(MyString, 500, 1)  ' Assume 500 is past the end of the string.

 MyVar = MyArray(387) ' Assume MyArray has less elements than that.
FixedArray:

 ' Custom Error
 If MyVar <> WhatIWantItToEqual Then
   Err.Raise 1000, ,"MyVar had the wrong value!"
 End If
ResumeControl:

 Exit Function

ErrorHandler:
 Select Case Err.Number
   Case 5: 'Invalid procedure call or argument, I think.
    If Len(MyString) < 500 Then
      MyString = Space(5000)
      Goto FunctionStart
    End If

   Case 9: 'Array out of bounds, if I recall correctly.
     MyVar = MyArray(UBound(MyArray))
     Goto FixedArray

   Case 1000: ' Our custom error.
     MyVar = WhatIWantItToEqual
     Goto ResumeControl

   Case Else
     Debug.Print "Oh no, an error of number " & Err.Number & " occured.  This means that you caused a(n) " & Err.Description & " error to happen."
 End Select
End Function


Anyway, I'm not trying to say that VB is the greatest language for all things and that it should always be used, just that it can be a lot more powerful than people give it credit for.

Edit: I typed this all out by hand and not in the VB IDE, so if you find errors in the code, bite me.

iago

So what you're saying is that you can do a lot of extra work, and have messy code, to implement something that other languages have by default?

And you missed my #1 problem: OOP.  I hate not having inheritance/polymorphism/ other OOP features.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


CupHead

It's not that much more work than other languages and keeping code clean is left to the programmer.  None of what I wrote was "dirty", as far as I can tell.  If you don't like the Gotos, then call the function again with your fixed parameter.  Oh, hard.  If you don't like a massive select case, put it in a function and call other functions rather than having the code pile up.  Those aren't even viable concerns.

VB has OOP through COM.  Go acronyms.  But anyway, you create a base class called an interface.  From other class modules, you use the keyword Implements [WhateverYourPreviousClassWas], and like magic, you have access to its properties and methods in your derived class.

Banana fanna fo fanna

#9
Those aren't pointers. Pointers are a syntax structure. And that function overloading is also a syntax construct; I don't want to have to write all of that.

I also think VB's error handling blows...period.

And, that isn't true OOP. Cup, I don't even think you know a semi-true OOP language.

iago

Yeah, that's not OOP, that's just inheritance.  It's missing the key features which I love using to keep my code organized and reusable.  Did you know that, in Java, I can use my same Bot class to connect to IRC, Battle.net, or Botnet, and I can do the login and event-handling stuff in subclasses?  It so clean and I can very easily add to it!

So, gogo OOP, boo VB for not having it.  
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


CupHead

Quote from: St0rm.iD on November 26, 2003, 04:11 PM
Those aren't pointers. Pointers are a syntax structure. And that function overloading is also a syntax construct; I don't want to have to write all of that.

I also think VB's error handling blows...period.

And, that isn't true OOP. Cup, I don't even think you know a semi-true OOP language.

Actually, I'm pretty sure a pointer is a 32-bit data structure (on most computers) that provides the address of the data you're looking for.  With respect to the function overloading, I don't see how Case vbString: <Code> Case vbLong: <Code> is any worse than:


ReturnType Whatever( char* Stuff)
{
 return;
}

ReturnType Whatever( long Stuff )
{
 return;
}


I guess you're entitled to your opinion about the error handling, but I don't see how it's that awful.

And lastly, I can agree that it's not 'true' OOP, but it can have essentially the same functionality.  If you want to bitch about polymorphism and inheritance, read the article.  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbdev00/html/vb00d20.asp

MyndFyre

A pointer is a data structure if you want to look at it that way, as you correctly stated how most 32-bit machines use 32-bit pointers.

However, what Storm was pointing out is that pointers are used syntactically in a different manner:


// C code
int myarr[] = { 5, 10, 20, 50, 100 };
int *ptr = &myarr;  // is this right?  I'm not a C programmer....
int i;
for (i = 0; i < 5; i++)
 printf("%d", *(myarr + (i * sizeof(int)));  // now I'm really wondering if this is correct....


However, with VB, IIRC, you don't actually use pointers - that is, there's no way to access the contents to which they point.  You can marshal your data to point to strings or objects because other languages (enabled with COM) require this; however, aside maybe from function callbacks, you don't actually use pointers.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mitosis

I like in C++ how when I make an error it will go specificly to the problem. However I may be wrong, but in my Visual Basic 6 when there is an error it will just say "compile error" or some shit and wont tell me what I did wrong in my code.

CupHead

Myndfyre, what are you talking about?


Dim x as String
Dim y as Long

y = StrPtr(x)


y is now the pointer to x, x has the contents of y.  It's like y = &x in C++.

Mitosis: I think you've confused your compilers.