Re: using a virtual C++ function
Re: using a virtual C++ function
- Subject: Re: using a virtual C++ function
- From: Robert Palmer Jr <email@hidden>
- Date: Tue, 10 Jun 2003 09:51:42 -0400
On Tuesday, June 10, 2003, at 04:13 AM, Florian G.. Pflug wrote:
On Mon, Jun 09, 2003 at 04:48:35PM -0400, Robert Palmer Jr wrote:
I have a C++ class with a virtual function that is expected to be
overridden.
class SensorFinder
{
public:
SensorFinder();
virtual ~SensorFinder();
void search();
virtual void sensorFound(char hwAddr[6]){};
}
class ObjCSensorFinder
{
public:
SensorFinder(id _delegate);
:SensorFinder::SensorFinder()
:delegate(_delegate)
{
[delegate retain] ;
}
virtual ~SensorFinder()
{
[delegate release] ;
}
virtual void sensorFound(char hwAddr[6])
{
[delegate foundSensor: hwAddr] ;
}
protected:
id delegate ;
}
Thanks everyone!!
After sending this request, I played some more and stumbled on this
very idea. The only reason mine didn't work was that I forgot to call
the init() function (see #2 below) - even better reason for using the
constructor to initialize the member variable. I implemented it a
little differently and had omitted two things.
1. I didn't have the [delegate retain/release] calls - why are they
needed?
2. I didn't set up delegate in the constructor, but rather in a
separate "init(id _delegate)" call - both work, the constructor method
is cleaner -- I changed mine.
I think there are a couple of typos in your code, but here's what I
ended up with and it works like a charm.
// ----- SEARCHER.H ------
#include "SensorFinder.h"
class Searcher: public SensorFinder
{
public:
Searcher(id _delegate);
~Searcher();
virtual void sensorFound(unsigned char hwAddr[]);
protected:
id delegate;
};
// ------ SEARCHER.MM ------
#import "Searcher.h"
#import "VideoSensor.h" // this is a sensor object
#import "V360LocatorController.h" // this is the delegate and defines
addSensor:sensor
#import "stdio.h"
Searcher::Searcher(id _delegate):
delegate(_delegate)
{
[delegate retain];
return;
}
Searcher::~Searcher()
{
[delegate release];
}
//------------------------------------------------------------
// Get notified when a sensor is found
// When a sensor is found, this will get called, providing the
// hardware address, IP address and product name.
void Searcher::sensorFound(unsigned char hwAddr[6])
{
VideoSensor* sensor = [[VideoSensor alloc] init];
char buf[32];
sprintf(buf, "x:x:x:x:x:x", hwAddr[0], hwAddr[1],
hwAddr[2], hwAddr[3], hwAddr[4], hwAddr[5]);
[sensor setMAC_Address: [[NSString alloc] initWithCString:buf]];
[delegate addSensor:sensor];
return;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.