[Gdal-dev] GDALDataset as a Base Class

Mateusz Loskot mateusz at loskot.net
Tue Aug 29 22:31:16 EDT 2006


Mateusz Loskot wrote:
> Mateusz Loskot wrote:
>> Matt Hanson wrote:
>>> Any ideas on what my derived class
>>> constructor should look like then?
>> I would use "named constructor idiom" and factory method:
>> Here is the concept in very short:
>>
>> class GSImage : public GDALDataset
>> {
>> public:
>>   static GSImage* Create(const char* file)
>>   {
>>      return static_cast<GSImage*>( GDALOpen(file, GA_ReadOnly ) );
>>   }
>> };
>>
>> GSImage* pImage = GSImage::create("myfile.png");
> 
> Let me unsay it ;-)
> The solution above forces you to still play with pointers.
> So, with current design of GDALDataset, it's much better to encapsulate
> GDALDataset as a private member of GSImage class than inheritance.
> Certainly, composition with its direct lifetime control.

Arghh, it's veeery late so I did a few typos....

As a lifetime control I mean, you create GDALDataset instance in the
GSImage ctor and opens it along. And in GSImage destructor you
closes it.

> Certainly, here you need to (re)design your own interface,
> as minimal as needed to use GSImage, or replicate whole
> interface after GDALDataset <--- may seem as a redundant work
> in comparison of encapsulation, but composition is much better
---------------^^^^^^^^^^^^^^^^^^ - it should be 'to inheritance'

OK, it's time to bed :-)

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Gdal-dev mailing list