POV-Ray : Newsgroups : povray.off-topic : Quick C++ question... : Re: Quick C++ question... Server Time
4 Sep 2024 21:24:03 EDT (-0400)
  Re: Quick C++ question...  
From: Warp
Date: 11 Nov 2009 19:00:19
Message: <4afb5013@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
>  > class dfbScreen::MyIDirectFB: public IDirectFB

> I'm not sure what this means, but I'm pretty sure it's not going to work 
> well. :-) DirectFB is all built on top of C, with fake OO stuff, so there's 
> no way to get one of these for real.

  Why wouldn't it work? dfbScreen::MyIDirectFB *is* IDirectFB (this is OOP,
after all). Wherever an object of type IDirectFB is expected, you can pass
a pointer to an object of type dfbScreen::MyIDirectFB. And yes, even if you
are passing it to a C function.

  Just to make sure, I made a complete test to see that, indeed, it works:

/*-----------------------------------------------------------
  SomeCStruct.h
  -----------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif

struct SomeCStruct
{
    int i, j;
};

void someCFunction(struct SomeCStruct* object);

#ifdef __cplusplus
}
#endif
/*-----------------------------------------------------------*/

/*-----------------------------------------------------------
  SomeCStruct.c
  -----------------------------------------------------------*/
#include "SomeCStruct.h"
#include <stdio.h>

void someCFunction(struct SomeCStruct* object)
{
    printf("i=%i, j=%i\n", object->i, object->j);
}
/*-----------------------------------------------------------*/

//-----------------------------------------------------------
// MyClass.hh
//-----------------------------------------------------------
class MyClass
{
    class SomeCStructWrapper;
    SomeCStructWrapper* obj;

    MyClass(const MyClass&);
    MyClass& operator=(const MyClass&);

 public:
    MyClass();
    ~MyClass();
    void foo();
};
//-----------------------------------------------------------

//-----------------------------------------------------------
// MyClass.cc
//-----------------------------------------------------------
#include "MyClass.hh"
#include "SomeCStruct.h"

class MyClass::SomeCStructWrapper: public SomeCStruct
{
 public:
    SomeCStructWrapper(const SomeCStruct& init): SomeCStruct(init) {}
};

MyClass::MyClass()
{
    SomeCStruct instance = { 5, 10 };
    obj = new SomeCStructWrapper(instance);
}

MyClass::~MyClass()
{
    delete obj;
}

void MyClass::foo()
{
    someCFunction(obj);
}
//-----------------------------------------------------------

//-----------------------------------------------------------
// test.cc
//-----------------------------------------------------------
#include "MyClass.hh"

int main()
{
    MyClass obj;
    obj.foo();
}
//-----------------------------------------------------------

  Compiled with:

gcc -Wall -O3 -c SomeCStruct.c
g++ -Wall -O3 MyClass.cc test.cc SomeCStruct.o

  Works just fine.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.