Click here to Skip to main content
15,913,905 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: particular area data to printer Pin
l a u r e n21-Oct-02 6:37
l a u r e n21-Oct-02 6:37 
GeneralCancelling a long process Pin
Mark Donkers21-Oct-02 4:18
Mark Donkers21-Oct-02 4:18 
GeneralRe: Cancelling a long process Pin
Ravi Bhavnani21-Oct-02 4:33
professionalRavi Bhavnani21-Oct-02 4:33 
GeneralRe: Cancelling a long process Pin
Mark Donkers21-Oct-02 5:21
Mark Donkers21-Oct-02 5:21 
Generalprinting particular area data to printer Pin
vgkotha21-Oct-02 4:17
vgkotha21-Oct-02 4:17 
GeneralDocument/View and WM_CHAR Pin
atanas.bakalov21-Oct-02 4:08
atanas.bakalov21-Oct-02 4:08 
Generaltemplate and ptr to it... Pin
Stan the man21-Oct-02 3:41
Stan the man21-Oct-02 3:41 
GeneralRe: template and ptr to it... Pin
Joaquín M López Muñoz21-Oct-02 8:45
Joaquín M López Muñoz21-Oct-02 8:45 
This can be done, but it is no trivial task. I'll sketch a basic scheme for a very simple data structure having only two member functions called put and get. As the host class (the one using the data structure) does not know in compile-time the exact type of the structure, this has to derive from an abstract base:
struct datastruct_base
{
  virtual ~datastruct_base()=0{}
  virtual void put(const void* arg)=0;
  virtual void get(void* arg)=0;
};
Note the argument to the functions has to be declared as raw pointers to void, since datastruct_base does not know about the actual types handled by its derived classes. A better approach could be taken using some sort of polymorphic variable like the one featured in Boost.Any[^].
Now, template classes derive from this abstract base like this:
template<typename type>
struct datastruct: public datastruct_base
{
  virtual void put(const void* arg)
  {
    put(static_cast<const type*>(arg));
  }
  virtual void get(void* arg)
  {
    get(static_cast<type*>(arg));
  }
  void put(const type* arg)
  {
    ...
  }
  void get(type* arg)
  {
    ...
  }
};
That's it. The host class sees the passed data structure through its base virtual class. For each instantiation of datastruct, put and get forward to non-virtual, type-specific member functions:
class host{
private:
  datastruct_base* ds;
public:
  void set_datastruct(datastruct_base* ds){this->ds=ds;}
  void put(const void* arg){ds->put(arg);}
  void get(void* arg){ds->get(arg);}
};
 
host hs;
datastruct<int> dsi;
hs.set(&dsi);
int i=10;
hs.put(&i);
hs.get(&i);
Hope you get the idea. This is a common idiom in modern C++, with roots stemming from a relatively old technique called envelope-letter. Good luck.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
GeneralGeting user privilege Pin
Stephbb7521-Oct-02 3:00
Stephbb7521-Oct-02 3:00 
Generalchange the colour of text in c++ Pin
rich Davis21-Oct-02 2:27
rich Davis21-Oct-02 2:27 
GeneralRe: change the colour of text in c++ Pin
Maximilien21-Oct-02 2:30
Maximilien21-Oct-02 2:30 
QuestionNULL and then reuse? Pin
ns21-Oct-02 2:18
ns21-Oct-02 2:18 
AnswerRe: NULL and then reuse? Pin
Hugo Hallman21-Oct-02 2:33
Hugo Hallman21-Oct-02 2:33 
GeneralRe: NULL and then reuse? Pin
Hugo Hallman21-Oct-02 2:34
Hugo Hallman21-Oct-02 2:34 
AnswerRe: NULL and then reuse? Pin
Maximilien21-Oct-02 2:36
Maximilien21-Oct-02 2:36 
Generalthanks! Pin
ns21-Oct-02 2:38
ns21-Oct-02 2:38 
GeneralRe: thanks! Pin
Christian Graus21-Oct-02 16:44
protectorChristian Graus21-Oct-02 16:44 
GeneralRe: thanks! Pin
ns22-Oct-02 1:15
ns22-Oct-02 1:15 
Questionhow monitor the keyboard message at one application Pin
misty21-Oct-02 1:39
misty21-Oct-02 1:39 
AnswerRe: how monitor the keyboard message at one application Pin
Christian Graus21-Oct-02 1:40
protectorChristian Graus21-Oct-02 1:40 
GeneralRe: how monitor the keyboard message at one application Pin
misty21-Oct-02 16:39
misty21-Oct-02 16:39 
GeneralRe: how monitor the keyboard message at one application Pin
Christian Graus21-Oct-02 16:43
protectorChristian Graus21-Oct-02 16:43 
GeneralRe: how monitor the keyboard message at one application Pin
misty21-Oct-02 17:22
misty21-Oct-02 17:22 
GeneralRe: how monitor the keyboard message at one application Pin
Christian Graus21-Oct-02 17:28
protectorChristian Graus21-Oct-02 17:28 
GeneralRe: how monitor the keyboard message at one application Pin
misty21-Oct-02 19:19
misty21-Oct-02 19:19 

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.