How to do that

V

Vince

Hello,

I have two classes FooA and FooB with exactly the same methods.
In my application I read a INI File and I am supposed to instantiate either
FooA or FooB.(Actually FooA and FooB
represents classes to control two differents smart card reader but with same
methods).

class FooA
{
int Init();
int Close();
}

class FooB
{
int Init();
int Close();
}




Myapp
-------
void *pPointer = NULL;
CIni ini; // Class to read a ini file

ini.ReadIni() // read ini file
IF ini.GetType == FooA
pPointer = new FooA
else
pPointer = new FooB

pPointer->Init()
pPointer->Close()

I try to do it by declaring pPointer as a void pointer but it didn't
compile.
How can I do that ??
 
P

Phlip

Vince said:
Hello,

I have two classes FooA and FooB with exactly the same methods.

Find a reason to give them a common base class.
In my application I read a INI File and I am supposed to instantiate either
FooA or FooB.(Actually FooA and FooB
represents classes to control two differents smart card reader but with same
methods).

Factory Pattern. Get the book /Design Patterns/ and read the entire thing
before you code or post again.
void *pPointer = NULL;
CIni ini; // Class to read a ini file

ini.ReadIni() // read ini file
IF ini.GetType == FooA
pPointer = new FooA
else
pPointer = new FooB

pPointer->Init()
pPointer->Close()

I try to do it by declaring pPointer as a void pointer but it didn't
compile.
How can I do that ??

You have rediscovered the need for abstract base classes. Look them up in
your tutorials.

FooA and FooB must inherit a common class; then pPointer can type to that
class.

You have also reinvented Factory Pattern, so you are ready to go read.
 
J

JKop

class FooA
{
int Init();
int Close();
}

class FooB
{
int Init();
int Close();
}

That won't compile. You must put in a ; after a class definition. You must
specify "public" (The default is "private"):

class FooA
{
public:

int Init();
int Close();
};

class FooB
{
public:

int Init();
int Close();
};


I presume that should be something like:

CIni ini("blah.ini");

ini.ReadIni() // read ini file
IF ini.GetType == FooA

Good God where do I start?

Google for "typeid".

Also that syntax should be:

if (ini.GetType() ==

(assuming "GetType" is a function)

pPointer = new FooA
else
pPointer = new FooB

pPointer->Init()
pPointer->Close()

pPointer->Init();
pPointer->Close();

Stick a ; after everything.



You've a lot of learning to do!


-JKop
 
V

Vince

Heu I wanted to do it simpler but I know all that. My problem is not a
syntax one because
I am already programing in C/C++ for 3 years.
Usually I only take from C++ what I miss from C : template, inheritage,
encapsulation.
But until this project I didn't need to use abstract class.
Thanks anyway
 
H

Howard

Vince said:
Heu I wanted to do it simpler but I know all that. My problem is not a
syntax one because
I am already programing in C/C++ for 3 years.
Usually I only take from C++ what I miss from C : template, inheritage,
encapsulation.
But until this project I didn't need to use abstract class.
Thanks anyway

Well, if your code looks like what you posted, then you DO have a syntax
problem. You should try to copy & paste your real code, not type it into
your email, because everyone here will focus on what you put in your email,
not what they *guess* your real code looks like.

(It looks like you need to do some reading before you go on, specifically
regarding inheritance, and class design.)

Basically, what you need is something like this:

class Foo
{
public:
virtual int Init() = 0;
virtual int Close() = 0;
virtual ~Foo(); // <--YOU NEED TO HAVE THIS!
};

class FooA : public Foo
{
public:
virtual int Init();
virtual int Close();
virtual ~Foo();
};

class FooB : public Foo
{
public:
virtual int Init();
virtual int Close();
virtual ~Foo();
};


....and later, in implementation code:

Foo* pFoo; // (you may want to add " = NULL;" here)
if (*whatever tells you to use FooA*)
pFoo = new FooA;
else
pFoo = new FooB;


Note the inclusion of a virtual destructor in the base class. You need that
if you're going to dynamically allocate instances of FooA or FooB using a
Foo* pointer, in order for the correct destructor to be called when you
later say "delete pFoo;". (Also, search groups.google.com for something
called the "Rule of Three". Basically, it tells you that you *may* also
need an assignment operator and a copy constructor for those classes.)

By the way, you might also look into something called "Resource Allocation
Is Initialization" (RAII). Whenever I see an Init() function, I immediately
wonder why. Initialization should, in general, be done in the constructor,
unless there are good reasons not to. You might want to have a member
object contained inside the Foo object that does in its constructor and
destructor what yyou're now doing in Init and Close. But that's something
you'll need to decide, after you read up on the subject.


-Howard
 
W

William Payne

Howard said:
Well, if your code looks like what you posted, then you DO have a syntax
problem. You should try to copy & paste your real code, not type it into
your email, because everyone here will focus on what you put in your
email,
not what they *guess* your real code looks like.

(It looks like you need to do some reading before you go on, specifically
regarding inheritance, and class design.)

Basically, what you need is something like this:

class Foo
{
public:
virtual int Init() = 0;
virtual int Close() = 0;
virtual ~Foo(); // <--YOU NEED TO HAVE THIS!
};

class FooA : public Foo
{
public:
virtual int Init();
virtual int Close();
virtual ~Foo();

Should be virtual ~FooA(); // But you know that =)
};

class FooB : public Foo
{
public:
virtual int Init();
virtual int Close();
virtual ~Foo();

Should be virtual ~FooB(); // But you know that =)

};


...and later, in implementation code:

Foo* pFoo; // (you may want to add " = NULL;" here)
if (*whatever tells you to use FooA*)
pFoo = new FooA;
else
pFoo = new FooB;


Note the inclusion of a virtual destructor in the base class. You need
that
if you're going to dynamically allocate instances of FooA or FooB using a
Foo* pointer, in order for the correct destructor to be called when you
later say "delete pFoo;". (Also, search groups.google.com for something
called the "Rule of Three". Basically, it tells you that you *may* also
need an assignment operator and a copy constructor for those classes.)

By the way, you might also look into something called "Resource Allocation
Is Initialization" (RAII). Whenever I see an Init() function, I
immediately
wonder why. Initialization should, in general, be done in the
constructor,
unless there are good reasons not to. You might want to have a member
object contained inside the Foo object that does in its constructor and
destructor what yyou're now doing in Init and Close. But that's something
you'll need to decide, after you read up on the subject.


-Howard

/ WP
 
V

Vince

Ok thanks for all this information. I am learning a lot with all of you.
You say that init function should be done in constructor but a constructor
doesn't have a return value so how do you know that your initialization went
well .
Usually in my init function I instanciate another Class and if soemthing
goes wrong I return an error code.
How can I do this with a constructor that doesn't return a value.

I know that I should read a good C++ book instead of asking stupid questions
but I prefer to ask and to see
answers because I must admit I am a bit lazy (Sorry).
But I want to change and I will try to read Bjarn Stroustrup book.
 
J

JKop

I know that I should read a good C++ book instead of asking stupid
questions but I prefer to ask and to see
answers because I must admit I am a bit lazy (Sorry).
But I want to change and I will try to read Bjarn Stroustrup book.


I wouldn't suggest Bjarne's book for the level you're at! I myself found it
difficult to read, it was very monotanous (where's spell-check when you need
it!). He introduced classes called "Stack" and I didn't even know what a
stack was (nor do I still) so I read about 1 in every 10 lines on those
pages.


-JKop
 
K

Karl Heinz Buchegger

Vince said:
Ok thanks for all this information. I am learning a lot with all of you.
You say that init function should be done in constructor but a constructor
doesn't have a return value so how do you know that your initialization went
well .
Usually in my init function I instanciate another Class and if soemthing
goes wrong I return an error code.
How can I do this with a constructor that doesn't return a value.

Eg. By introducing a member function which tells the caller if the
object is in a good state.

Or you could eg. throw an exception

I know that I should read a good C++ book instead of asking stupid questions
but I prefer to ask and to see
answers because I must admit I am a bit lazy (Sorry).

You won't get very far with that.
C++ as language and the way it is used is too complex for that.
C++ not only lives with the language but also with what we call
'idioms'. That is: typical constructs used for special needs.
RAII is such an idiom as well as the 'factory pattern' or the
'virtual constructor' or ....
 
R

Ron Natalie

JKop said:
I wouldn't suggest Bjarne's book for the level you're at! I myself found it
difficult to read, it was very monotanous (where's spell-check when you need
it!). He introduced classes called "Stack" and I didn't even know what a
stack was (nor do I still) so I read about 1 in every 10 lines on those
pages.

1. Spell check is available in a variety of news posters. May
I suggest you switch from XNews to Thunderbird (which with some
of the extensions like mhenhy, is very nice..).

2. Bjarne's book assumes you can program, just not in C++. If
you don't know what a stack is, you might try a basic CS book
like Horowitz and Sahni or such. You can't even begin to understand
concepts like containers without it really.
 
J

JKop

1. Spell check is available in a variety of news posters. May
I suggest you switch from XNews to Thunderbird (which with some
of the extensions like mhenhy, is very nice..).

Thanks, will look into it.
2. Bjarne's book assumes you can program, just not in C++. If
you don't know what a stack is, you might try a basic CS book
like Horowitz and Sahni or such. You can't even begin to understand
concepts like containers without it really.

hmm... effort... uhh


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top