Re: Obj-C++ Tuturial Anywhere?
Re: Obj-C++ Tuturial Anywhere?
- Subject: Re: Obj-C++ Tuturial Anywhere?
- From: "Dennis C. De Mars" <email@hidden>
- Date: Wed, 15 Aug 2001 23:19:07 -0000
Andreas Monitzer <email@hidden> said:
>
On Thursday, August 16, 2001, at 12:03 , Todd Blanchard wrote:
>
>
> 5) The only other good use might be the following "smart releasing
>
> pointer"
>
> class:
>
>
>
> class Id
>
> {
>
> id _object;
>
> Id(id obj) : _object([obj retain]) {}
>
> ~Id() { [_object release]; }
>
> operator id() { return _object; }
>
> }
>
>
>
> ...
>
> {
>
> Id foo = [MyClass newObject];
>
>
>
> } // destructor releases foo
>
>
>
> This might not work though - I haven't tried it as I don't have the new
>
> thingy.
>
>
Correct C++ is:
>
>
Id foo([MyClass newObject]);
No, actually his statement is a legal alternative syntax. That is to say, if you have a
constructor:
TheClass::TheClass(X arg);
then declaring a variable of class TheClass, you can write
TheClass var(arg);
or
TheClass var = arg;
This only works if the constructor does not have more than one required
argument, otherwise you have to use the first syntax.
However, I think this class might not work if [MyClass newObject] has a return
type other then id; although Objective C statements recognize that any Objective
C class can be cast to "id", I am guessing the the C++ function overloading logic
would not recognize this. Of course, this discussion is pretty academic, I don't
think many people are going to be interested in doing this sort of thing...
Dennis D.