Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Another pointer question...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Another pointer question...



Am 15.07.2010 um 05:13 schrieb Jennifer Usher:
> I am trying to port some C code libraries into a program I am working on and I am a bit puzzled how to convert this function declaration:
>
> void dateYMD(int nDate, int &year, int &month, int &day, calendar dateSystem=cal_Gregorian)

 That code uses C++ features. The "&" is a reference, essentially the same as the "*"-operator, just that you can use it without dereferencing. I.e. to convert C++ references to C (and thus Objective-C) pointers, you'd take:

void main()
{
    int		x = 100;
    changeX( x );
}

void  changeX( int &x )
{
    x = 15;
}

and turn it into

void main()
{
    int		x = 100;
    changeX( &x );
}


void  changeX( int *x )
{
    *x = 15;
}


Similarly for a struct MyStruct { int number }; you'd turn:

void main()
{
    MyStruct	x;
    changeX( x );
}


void  changeX( MyStruct &x )
{
    x.number = 15;
}


into:

void main()
{
    struct MyStruct	x;
    changeX( &x );
}


void  changeX( MyStruct *x )
{
    x->number = 15;	// or (*x).number = 15;
}


Similarly, the calendar dateSystem=cal_Gregorian is a default parameter. That is, when calling this function in C++, people can leave out that parameter, and the compiler will automatically generate code as if you had passed cal_Gregorian. To fix that, you'd just remove that part and everywhere you call that function, you'd pass cal_Gregorian as an additional last parameter. Alternately, you can create another function that doesn't have the last parameter:

void dateYMDGregorian(int nDate, int *year, int *month, int *day)
{
    dateYMD(nDate,year,month,day,cal_Gregorian);
}

And then just change the call sites to call dateYMDGregorian. Alternately, you can turn on the Objective-C++ compiler, which lets you mix C++ and Objective C in the same file.

-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Another pointer question... (From: Jennifer Usher <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.