Click here to Skip to main content
16,004,901 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Pointer to another Window Pin
tjkrz10-Dec-03 8:44
tjkrz10-Dec-03 8:44 
GeneralRe: Pointer to another Window Pin
Antti Keskinen11-Dec-03 7:10
Antti Keskinen11-Dec-03 7:10 
GeneralRe: Pointer to another Window Pin
David Crow10-Dec-03 7:47
David Crow10-Dec-03 7:47 
GeneralRe: Pointer to another Window Pin
Joel Lucsy11-Dec-03 4:08
Joel Lucsy11-Dec-03 4:08 
GeneralRe: Pointer to another Window Pin
tjkrz15-Dec-03 9:21
tjkrz15-Dec-03 9:21 
Generaltemplate design question Pin
pankajdaga10-Dec-03 6:30
pankajdaga10-Dec-03 6:30 
GeneralRe: template design question Pin
Jörgen Sigvardsson10-Dec-03 11:34
Jörgen Sigvardsson10-Dec-03 11:34 
GeneralRe: template design question Pin
pankajdaga10-Dec-03 12:50
pankajdaga10-Dec-03 12:50 
I figured out what my mistake was:

- The destructor of the policy class is supposed to be protected, not the derived class. Basically, Andrei breaks from the rule that Inheritance behavior is IS-A. he rather prefers..Inheritance as USES. I would probably disagree with him on that.

Just to be clear, let me create a small example:

Recently, I had to write a small module and that reads a binary file generated by some system, extract the data, and put it in an SQL server database. I had to do it in C++. The trick is to try and see which method suits the best for the particular design:

- I wanted the design to be a bit generic, so people could expand on it. So I had a base class like so (for simplicity let us say it has only one method for writing to database called WriteExperiment():

class DataSourceBase
{
public:
DataSourceBase();
virtual ~DataSourceBase();
virtual bool OpenSource(std::string & connectionStr);
virtual bool CloseSource();
virtual bool WriteExperiment(int data);
};

Now, for my need I inherit from this class:

class SqlServerSource: public DataSourceBase
....
// You know the drill...

Then I have a writer class basically which takes a reference to a DataSourceBase object and polymorphism does the rest.

With the template based method, I would do the same as:

class SqlServerSource
{
public:
SqlServerSource()
{}
bool OpenSource(std::string & connectionStr){}
bool CloseSource() {return true}
bool IsOpen()
{
return true;
}
protected:
~SqlServerSource(){}
};

template <class DataSource>
class WriteExperimentPolicy
{
public:
WriteExperimentPolicy() {}
void SetDataSource(DataSource * source)
{
m_pDataSource = source;
}
bool WriteExperiment(int val)
{
// Do something here
return true;
}
private:
DataSource * m_pDataSource;
protected:
~WriteExperimentPolicy();
};

// My class which uses both of them
template <class DataSource, class WritePolicy>
class MySource: public DataSource, WritePolicy
{
public:
MySource()
{}

void Test()
{
if (this->IsOpen())
this->WriteExperiment(1);
}
};

// In the main application:

typedef MySource<SqlServerSource, WriteExperimentPolicy<SqlServerSource> >Source;

int main()
{
Source temp;
temp.Test();
return 0;
}

I have just read the first chapter, but it looks like I can learn a lot from this book. I would never even think of doing this before. I was very hesitant with templates and never used it much. I am a very novice programmer and got miles to go still Smile | :)

Pankaj

Without struggle, there is no progress
GeneralRe: template design question Pin
Andrew Walker10-Dec-03 12:44
Andrew Walker10-Dec-03 12:44 
GeneralRe: template design question Pin
pankajdaga10-Dec-03 12:52
pankajdaga10-Dec-03 12:52 
QuestionHyperthreading Windows API? Pin
solla10-Dec-03 6:03
solla10-Dec-03 6:03 
AnswerRe: Hyperthreading Windows API? Pin
valikac10-Dec-03 6:20
valikac10-Dec-03 6:20 
GeneralRe: Hyperthreading Windows API? Pin
solla10-Dec-03 6:24
solla10-Dec-03 6:24 
GeneralRe: Hyperthreading Windows API? Pin
David Crow10-Dec-03 8:50
David Crow10-Dec-03 8:50 
GeneralRe: Hyperthreading Windows API? Pin
solla11-Dec-03 1:43
solla11-Dec-03 1:43 
GeneralCreating and Using Static lib Pin
Anonymous10-Dec-03 5:31
Anonymous10-Dec-03 5:31 
GeneralRe: Creating and Using Static lib Pin
Larry J. Siddens10-Dec-03 6:00
Larry J. Siddens10-Dec-03 6:00 
GeneralRe: Creating and Using Static lib Pin
Chris Meech10-Dec-03 6:07
Chris Meech10-Dec-03 6:07 
GeneralMultithreading using _beginthread Pin
madmurdz10-Dec-03 4:54
madmurdz10-Dec-03 4:54 
GeneralRe: Multithreading using _beginthread Pin
Alexander M.,10-Dec-03 5:16
Alexander M.,10-Dec-03 5:16 
GeneralRe: Multithreading using _beginthread Pin
John M. Drescher10-Dec-03 5:38
John M. Drescher10-Dec-03 5:38 
GeneralRe: Multithreading using _beginthread Pin
valikac10-Dec-03 6:22
valikac10-Dec-03 6:22 
GeneralRe: Multithreading using _beginthread Pin
Jörgen Sigvardsson10-Dec-03 8:17
Jörgen Sigvardsson10-Dec-03 8:17 
GeneralRe: Multithreading using _beginthread Pin
Gary R. Wheeler10-Dec-03 15:11
Gary R. Wheeler10-Dec-03 15:11 
GeneralRe: Multithreading using _beginthread Pin
David Crow11-Dec-03 2:31
David Crow11-Dec-03 2:31 

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.