Click here to Skip to main content
15,921,276 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: SQL ? Pin
Matt Gullett26-Jul-01 2:09
Matt Gullett26-Jul-01 2:09 
AnswerRe: SQL ? Pin
Hadi Rezaee27-Jul-01 18:18
Hadi Rezaee27-Jul-01 18:18 
GeneralRe: SQL ? Pin
Jamie Nordmeyer27-Jul-01 20:22
Jamie Nordmeyer27-Jul-01 20:22 
GeneralRe: SQL ? Pin
Hadi Rezaee27-Jul-01 20:45
Hadi Rezaee27-Jul-01 20:45 
GeneralRe: SQL ? Pin
Jamie Nordmeyer27-Jul-01 22:36
Jamie Nordmeyer27-Jul-01 22:36 
GeneralRe: SQL ? Pin
Hadi Rezaee28-Jul-01 18:58
Hadi Rezaee28-Jul-01 18:58 
Generallogging winsock send/receive Pin
Kuniva26-Jul-01 0:30
Kuniva26-Jul-01 0:30 
Generalclass design question Pin
26-Jul-01 0:25
suss26-Jul-01 0:25 
Hi,

i have a question on class design. I want to build a simple class to save
inifile settings in a binary fle. I started designing and made a class called
CInitfile. This class can only be used to derive classes from s then you
end up with someting like CRecStudioInitfile. Mind i'm still in the design
phase of things so the focus is on the classes, not on the code.
No i have a number of questions and i have marked the code that is related
to the question with a tag like (1) so you can see the part of the code i'm
talking about.

(1) If no arguments are passed, i want the constructur to make an init file
with the default name as specified by INIT_FILENAME. If a name is specified
i want to assign a name. Is the way i'm doing that ok in the base class?
What about the derived class?

(2) What should happen then is that in the constructor, all parameters should be initialised. Since i have 2 constructors and wanted to avoid placing the same code in both functions, i wanted to call another function from both constructors.
This initialisation is a necessity for me, so it seems like i need to specify
a pure virtual function in the base class so the derived classes woul have to build their own version. That was all going ok, but then i want to force this function to be loaded automatically by the constructor. I wanted to define this in the base class but i got an error and had to add (3) in the cpp file.

So i wanted to make sure that the baseclass initializes m_szFilename with either the default name or a name that is specified and after that, forces a function (here InitialiseParameters() ) to be called so that you are sure the memberdata is initialised. By making this function InitialiseParameters() virtual i was hoping to make sure that the derived classes should have one and by putting this in the baseconstructor i wanted to make sure the InitialiseParameters() of the derived class was called.

How do i encorporate such a design?

Thanks.

================== initfile.h ==================
#ifndef _INITFILE_H_
#define _INITFILE_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include "errorexc.h"
#include "resource.h"


#define INIT_FILENAME _T("logfile.txt") // (1)


class CInitfile {
// since we don not want people to use this class immediately (they need
// to derive a class from this base class)
protected:
// constructor
CInitfile() { CInitfile(INIT_FILENAME); }; // (1)
CInitfile(const _TCHAR *szFilename) : m_szFilename(szFilename) { InitialiseParameters(); }; // (1)

// destructor
virtual ~CInitfile() {};

public:
// Pure virtual functions
virtual BOOL ReadInitfile() const = 0 ; // force derived class to make his own readfile
virtual BOOL WriteInitfile()= 0; // force derived class to make his own writefile
const _TCHAR* GetFilename() {return m_szFilename;};

private:
virtual void InitialiseParameters()=0; // this should be defined in all derived classes //(2)
const _TCHAR *m_szFilename; //filename of the file that is used to store/restore parameters too

};


class CRecStudioInitfile : public CInitfile
{
public:
// constructor & destructor
CRecStudioInitfile() {}; // (1)
CRecStudioInitfile(const _TCHAR *szFilename) : CInitfile(szFilename){}; // (1)
virtual ~CRecStudioInitfile();

BOOL ReadInitfile() const;
BOOL WriteInitfile();

// private data is all the data we want to save
private:
void InitialiseParameters(); //(2)
BOOL m_bLogMemoryToFile;
// other datamembers to come here
};

#endif // ifndef _INITFILE_H_
================== initfile.h ==================


================== initfile.cpp ==================
#include "stdafx.h"
#include "initfile.h"

// had to do this to avoid compile errors
void CInitfile::InitialiseParameters(){}; // (3)

CRecStudioInitfile::~CRecStudioInitfile()
{
}

BOOL CRecStudioInitfile::ReadInitfile() const
{
// no code yet
return TRUE;
}

BOOL CRecStudioInitfile::WriteInitfile()
{
// no code yet
return TRUE;
}

void CRecStudioInitfile::InitialiseParameters()
{
m_bLogMemoryToFile = FALSE;
// other data that needs to be initialised
}
================== initfile.cpp ==================
GeneralRe: class design question Pin
Tomasz Sowinski26-Jul-01 2:50
Tomasz Sowinski26-Jul-01 2:50 
GeneralRe: class design question Pin
26-Jul-01 2:58
suss26-Jul-01 2:58 
GeneralRe: class design question Pin
Tomasz Sowinski26-Jul-01 3:09
Tomasz Sowinski26-Jul-01 3:09 
GeneralRe: class design question Pin
26-Jul-01 3:57
suss26-Jul-01 3:57 
GeneralRe: class design question Pin
Tomasz Sowinski26-Jul-01 4:25
Tomasz Sowinski26-Jul-01 4:25 
GeneralRe: class design question Pin
26-Jul-01 6:04
suss26-Jul-01 6:04 
Generalclass design question Pin
26-Jul-01 0:24
suss26-Jul-01 0:24 
Questionwhats wrong here?? Pin
26-Jul-01 0:14
suss26-Jul-01 0:14 
AnswerRe: whats wrong here?? Pin
Steve T26-Jul-01 12:24
Steve T26-Jul-01 12:24 
GeneralOwner Drawn Buttons Pin
Derek Lakin25-Jul-01 23:22
Derek Lakin25-Jul-01 23:22 
GeneralRe: Owner Drawn Buttons Pin
jerry0davis25-Jul-01 23:33
jerry0davis25-Jul-01 23:33 
GeneralRe: Owner Drawn Buttons Pin
Derek Lakin26-Jul-01 1:45
Derek Lakin26-Jul-01 1:45 
GeneralRe: Owner Drawn Buttons Pin
Paolo Messina26-Jul-01 11:37
professionalPaolo Messina26-Jul-01 11:37 
GeneralRe: Owner Drawn Buttons Pin
Derek Lakin26-Jul-01 21:20
Derek Lakin26-Jul-01 21:20 
QuestionECVT() and FCVT()? Pin
Gérald Mercet25-Jul-01 23:11
Gérald Mercet25-Jul-01 23:11 
AnswerRe: ECVT() and FCVT()? Pin
Gerry26-Jul-01 0:12
Gerry26-Jul-01 0:12 
GeneralI need Help - Dialog Box Pin
Leezo25-Jul-01 21:47
Leezo25-Jul-01 21:47 

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.