Click here to Skip to main content
15,912,204 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionProblem of Print Preview. Pin
Le@rner9-Jul-08 18:13
Le@rner9-Jul-08 18:13 
AnswerRe: Problem of Print Preview. Pin
Iain Clarke, Warrior Programmer10-Jul-08 0:06
Iain Clarke, Warrior Programmer10-Jul-08 0:06 
NewsLook for a friend Pin
halibobo9-Jul-08 17:52
halibobo9-Jul-08 17:52 
GeneralRe: Look for a friend [modified] Pin
_AnsHUMAN_ 9-Jul-08 18:52
_AnsHUMAN_ 9-Jul-08 18:52 
GeneralRe: Look for a friend Pin
halibobo9-Jul-08 20:54
halibobo9-Jul-08 20:54 
QuestionCrashing at RegQueryValueEx Pin
monsieur_jj9-Jul-08 16:00
monsieur_jj9-Jul-08 16:00 
AnswerRe: Crashing at RegQueryValueEx Pin
krmed10-Jul-08 2:14
krmed10-Jul-08 2:14 
QuestionC++ wrappers for C struct api Pin
Kishore Jonnalagadda9-Jul-08 15:14
Kishore Jonnalagadda9-Jul-08 15:14 
Hi all,

I am new to this forum and generally any programming help forum for that matter.

I have a case where i want to maintain both C and C++ api library. So therefore i choose to make my C++ support a thin wrapper around the C code. However, i do not know what is the most effective way to achieve this without unnecessary memory and performance overhead. Below is an example case.

/*Begin Code Sample*/

/*Start time.h*/

struct Time
{
int seconds,
int nanoseconds
};

void timeAdd(struct Time* time, struct Time* delta);
void timeSub(struct Time* time, struct Time* delta);

/*End time.h*/

/*Start timer.h*/

struct Timer
{
struct Time time;
void (*callback)(struct Timer *timer);
};

void timerSetCallback(struct Timer *timer, void (*callback)(struct Timer *timer));
void timerStart(struct Timer *timer);
void timerStop(struct Timer *timer);

/*End timer.h*/

/*Start time.hxx*/

namespace C
{
#include "time.h"
}

class Time
{
public:
Time()
{
m_time.seconds = 0;
m_time.nanoseconds = 0;
};
~Time(){};
int seconds(){return m_time.seconds;}
int nanoseconds(){return m_time.nanoseconds;}
void setSeconds(int seconds){m_time.seconds = seconds};
void setNanoseconds(int nanoseconds){m_time.nanoseconds = nanoseconds};
void add(Time &time){C::timeAdd(&m_time, &time.m_time);};
void sub(Time &time){C::timeSub(&m_time, &time.m_time);};
protected:
C::Time m_time;
};

/*End time.hxx*/

/*Start timer.hxx*/
#include "timer.h"

class Timer : public Time
{
public:
Timer():Time(){m_timer.callback = &Timer::callback;};
~Timer(){C::timerStop(&m_timer);};

start(){m_timer.time = m_time; C::timerStart(&m_timer);};
stop(){C::timerStop(&m_timer);};
protected:
static void callback(struct Timer *timer);
C::Timer m_timer;
};


/*End Code Sample*/

Now while this works for this simple case, it has a few drawbacks like the additional memory used to allocate m_time in class Time when all of struct Time api's would have even worked for struct Timer and hence struct Time m_time could have been "replaced" by struct Timer m_time but instead both exist in class Timer.

It also is the simpler of the cases where the struct sizes are relatively small and the call to Timer::start() was easily managed by a copy of the struct.

So now, my question is how can c++ wrappers be written for such cases without the additional memory overhead? Given their similarity, I definitely want class Timer to be a subclass of class Time!

However, i do have the flexibility to modify both the C and C++ code but i need both the C and C++ code to be equally capable, clean and efficient. How can i do it?
AnswerRe: C++ wrappers for C struct api Pin
Saurabh.Garg9-Jul-08 17:57
Saurabh.Garg9-Jul-08 17:57 
GeneralRe: C++ wrappers for C struct api Pin
Kishore Jonnalagadda9-Jul-08 18:25
Kishore Jonnalagadda9-Jul-08 18:25 
GeneralRe: C++ wrappers for C struct api Pin
Saurabh.Garg9-Jul-08 23:25
Saurabh.Garg9-Jul-08 23:25 
GeneralRe: C++ wrappers for C struct api Pin
Kishore Jonnalagadda10-Jul-08 0:11
Kishore Jonnalagadda10-Jul-08 0:11 
GeneralRe: C++ wrappers for C struct api Pin
Saurabh.Garg10-Jul-08 0:56
Saurabh.Garg10-Jul-08 0:56 
AnswerRe: C++ wrappers for C struct api Pin
BadKarma9-Jul-08 22:54
BadKarma9-Jul-08 22:54 
GeneralRe: C++ wrappers for C struct api Pin
Kishore Jonnalagadda9-Jul-08 23:54
Kishore Jonnalagadda9-Jul-08 23:54 
Questioncall a vbscript and get an output Pin
rocktx9-Jul-08 9:32
rocktx9-Jul-08 9:32 
AnswerRe: call a vbscript and get an output Pin
led mike9-Jul-08 10:10
led mike9-Jul-08 10:10 
QuestionGDI+ Region Outline question Pin
Iain Clarke, Warrior Programmer9-Jul-08 5:44
Iain Clarke, Warrior Programmer9-Jul-08 5:44 
AnswerRe: GDI+ Region Outline question Pin
Mark Salsbery9-Jul-08 6:21
Mark Salsbery9-Jul-08 6:21 
GeneralRe: GDI+ Region Outline question Pin
Iain Clarke, Warrior Programmer9-Jul-08 11:36
Iain Clarke, Warrior Programmer9-Jul-08 11:36 
QuestionRetriving the user privilage set using WINAPI Pin
vineeshV9-Jul-08 3:17
vineeshV9-Jul-08 3:17 
AnswerRe: Retriving the user privilage set using WINAPI Pin
Iain Clarke, Warrior Programmer9-Jul-08 4:47
Iain Clarke, Warrior Programmer9-Jul-08 4:47 
GeneralRe: Retriving the user privilage set using WINAPI Pin
David Crow9-Jul-08 5:03
David Crow9-Jul-08 5:03 
Questionfriend member function in VC++ 2008 Pin
Andy Rama9-Jul-08 2:45
Andy Rama9-Jul-08 2:45 
AnswerRe: friend member function in VC++ 2008 Pin
Nibu babu thomas9-Jul-08 2:53
Nibu babu thomas9-Jul-08 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.