![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Or even
bool CharCon::eq(Comparator &x)
{
return (character == ((CharCon &)x).getValue());
}Note that this means you are telling the compiler that x is always a CharCon - there is nothing to stop you passing in a different derived class to the eq function and your program will probably crash. |
|
|
|
|
|
#12 |
|
Newbie
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0
![]() |
Got the tree working now
I am now considering using a stack to keep all the 'new' pointers together, in order to delete them at the end of the program.Thanks for all the help folks. |
|
|
|
|
|
#13 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
If you want a bit more safety, you can also check the the Comparator is a CharCon like this;
bool CharCon::eq(Comparator &x)
{
if (dynamic_cast<CharCon *>(&x) != (CharCon *)NULL)
{
return (character == ((CharCon &)x).getValue());
}
else
{
do_something_else();
}
}It is probably a good idea to provide a virtual destructor (even if it does nothing) in the Comparator class as well. Not important for the current problem, but saves difficulties with other types of problem. |
|
|
|
|
|
#14 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#15 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#16 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
No. What I mean is that there is nothing preventing a class that declares a pure virtual function from also implementing it.
For example; #include <iostream>
class Base
{
public:
Base();
virtual ~Base() = 0;
virtual void Operation() = 0;
};
class Derived : public Base
{
public:
Derived();
~Derived();
void Operation();
}
// implement member functions
Base::Base()
{
std::cout << "Constructing Base\n";
}
Base::~Base() // We are actually REQUIRED to implement a destructor we declare even if it is pure virtual
{
std::cout << "Destructing Base\n";
}
void Base::Operation() // YES, we are allowed to implement a pure virtual function
{
std::cout << "Operating on Base\n";
}
Derived::Derived() : Base() // explicitly initialising Base is optional
{
std::cout << "Constructing Derived\n";
}
Derived::~Derived()
{
std::cout << "Destructing Derived\n";
}
void Derived::Operation()
{
Base::Operation(); // we can explicitly call a pure virtual function if we have implemented it
std::cout << "Operating on Derived\n";
}
int main()
{
Base *base = new Base; // remove this line as it will result in compiler error as Base is abstract
Derived *derived = new Derived; // OK. We will see that Base's constructor is invoked, then Derived's
derived->Operation(); // OK. We will see that Base::Operation is called by Derived::Operation()
derived->Base::Operation() // I'll leave working out what this does as an exercise....
delete derived; // Derived's destructor is invoked then Base's
} |
|
|
|
|
|
#17 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, I certainly learned something there. Thanks.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#18 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
![]() |
|
|
|
|
|
|
#19 |
|
Expert Programmer
|
Since nobody seems to have mentioned it, I'm just wondering if everyone is aware that you can declare abstract classes in Java as well, and from what I understand from reading grumpy's post, they are essentially the same in both languages.
|
|
|
|
|
|
#20 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
Abstract classes will behave similarly between the two languages; however, in Java, you have to use the abstract keyword if you define a class that has a "pure virtual" (i.e. not yet defined) function.
C++: class abs // no special keywords on this line
{
public:
abs(){}
virtual int something() = 0;
};
class full : public abs
{
public:
full(){}
int something() { return 1; }
};public abstract class abs // have to have abstract here
{
public abs() {}
public abstract int something();
}
public class full extends abs
{
public full() {}
public int something() { return 1; }
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|