Welcome, Guest. Please login or register.

Author Topic: Amiga 3.9 Includes in C++  (Read 2581 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Amiga 3.9 Includes in C++
« on: July 21, 2004, 03:34:50 PM »
Hmm, not sure I follow you.

C++ has no problem with void* at all, in fact, C++ introduced the void pointer, which C later adopted (rather than using unsigned char* etc).

I certainly have experienced no problems with the OS3.5 or 3.9 includes relating to void*. In fact the only problems I have ever encountered was a scope of resolution problem to do with library base names that happen to have global pointers of the same name (eg IntuitionBase* IntuitionBase), which was resolved by using :: operator to prefix the pointer.

What C++ doesn't like is implicit pointer conversion from void, which is allowed in C, or from any type to another that isn't defined directly or in terms of base/subclass heirarcchy.

So you can't do things like

int* n = AllocVec(1024, MEMF_PUBLIC);

since AllocVec returns APTR (usually typedefed' as void*).

The OS was written with in C in mind, and as such has some of C's legacy problems that mean some example sources may not compile out of the box in C++ mode, but is quite managable from C++ provided you spot any violations of C++ typing rules.

It sounds like you have a configuration problem. Something I hate about gcc is that it is such a {bleep} to set up usually...

Good luck :-)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Amiga 3.9 Includes in C++
« Reply #1 on: July 21, 2004, 05:13:47 PM »
Hi,

You don't need to redefine anything. You just have to manually cast from void* to type* in C++ since this implicit conversion is forbidden (part of C++'s stricter rules).

To use eg. AllocVec(), cast the return value to the pointer type you require, eg

int* n = (int*)AllocVec(1024, MEMF_PUBLIC);

...and that's it. You really don't need to modify any of the headers or anything like that - in fact doing so is a very bad idea since you are making your code dependent on non standard (no matter how much you don't like the defined standard itself ;-)) amigaos headers.

-edit-

Note it's not just amigaos that has this problem. Even the C standard library malloc(), calloc() and realloc() all return void* and so need manual casts in C++ if they are to be assigned to a typed pointer (like int* etc).

Essentially, casting from void* to type* is dangerous which is why C++ banned implicit conversion. When you explicitly cast as above, you as the programmer are taking responsibility.

-/edit-

As it goes, for simple allocations of this kind and stuff you should really use new[] and delete[] anyway. Use AllocVec() when working close to the OS, or have a specialist requirement (eg allocating memory in chip ram or whatever - I've personally created my own new[] operators for this kind of stuff in the past.
int p; // A