|
Boost Users : |
From: Stephen torri (storri_at_[hidden])
Date: 2005-02-09 23:02:50
On Wed, 2005-02-09 at 20:02 -0700, Welson Sun wrote:
> Hi,
>
> I am new to C++ and I am converting a Java project to C++. Here is the
> error I got:
I believe I see your problem here. You want OperatorType to be a pure
virtual class. Well you cannot use a class that has a pure virtual
function you cannot create an object of that type. The purpose of a
class with pure virtual functions is to be a base class like you are
using with OperatorType. So we need to change your code ignore to make
it say that OperatorType is a base class which BasicOperatorType
inherits from:
<BEFORE>
class OperatorType
{
public:
virtual string getMnemonic(void);
};
<AFTER>
class OperatorType
{
public:
virtual string getMnemonic(void) = 0;
};
Quote from: http://www.firststep.com.au/jlc/javacc/virtual.html
While we're on the subject of virtual functions ...
Java, like C++, supports pure virtual, or abstract, functions
(Base class functions which must be defined in derived classes).
In C++, the cryptic notation "=0" is used at the end of the
function declaration. Java uses the more sensible abstract
keyword.
If your class has any abstract member functions, Java also
insists that you declare the class as abstract. This advertises
that it's an abstract class (In C++, you have to look through
the member functions and notice that there's at least one pure
virtual function).
So we repeat this change for class Operator as well.
So we are not done yet changing your code. We need to change the
OperatorType and Operator declarations to be pointer types, e.g.
OperatorType*. Since you cannot define a Operator type because a
function is declared pure virtual you can still use it as a pointer.
What you are saying is that the pointer is of type Operator but in fact
is a class that supports the Operator interface.
<BEFORE>
// NOTE: This is what the class Operator would look like after making
// the change described above. Its function getOperatorType is
// now a pure virtual function which anything wanting to support
// the Operator interface must define.
class Operator{
public:
virtual OperatorType getOperatorType() = 0;
};
<AFTER>
class Operator{
public:
virtual OperatorType* getOperatorType() = 0;
};
So anything wanting to support the Operator interface must have a
function called getOperatorType that returns a pointer to a class which
supports the OperatorType interface. Does that make sense?
The final step after making these changes is to use these new classes.
static OperatorType* portType = new BasicOperatorType("Port");
Here is a variable that is a OperatorType pointer called portType. Since
BasicOperatorType supports the OperatorType we can assign a pointer on
the heap of type BasicOperatorType to the portType variable.
The only operations supported on portType is the getMnemonic() function.
I would recommend two books:
C++ Primer Plus - 4th Edition (or latest) from Stephen Prata.
The C++ Programming Language (Special 3rd Edition)
by Bjarne Stroustrup.
I use the first one but a good friend of my likes the other. Both are
good guides for getting into the C++ programming language.
Cheers!
Stephen
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net