Home » U++ Library support » U++ Core » Derivating from Vector<>
Derivating from Vector<> [message #8343] |
Sat, 03 March 2007 20:12 |
victorb
Messages: 78 Registered: December 2005 Location: Nice, France
|
Member |
|
|
I am trying to derivate a class from Vector<...> but I can't get it to work. Any help from the Upp community would be welcome.
Here is some sample code:
#include <Core/Core.h>
using namespace Upp;
class IntVector : public DeepCopyOption<IntVector, Vector<int> >
{
public:
IntVector(){Cout() << "IntVector\n"; }
virtual ~IntVector(){Cout() << "~IntVector\n";}
IntVector(const IntVector &src, int) {
::new IntVector;
Vector<int>(src, 0);
name = src.name;
Cout() << "DCC\n";
}
String name;
String ToString(void) {
String dump;
dump << name << " ";
if (IsPicked()) return dump << "Picked";
for (int i = 0; i < GetCount(); i++) {
dump << At(i) << " ";
}
return dump;
}
};
CONSOLE_APP_MAIN
{
Cout() << "iv\n";
IntVector iv;
iv.name = "IV";
iv.Add(5);
iv.Add(6);
Cout() << "iv2\n";
IntVector iv2(iv, 0);
Cout() << iv.ToString() << "\n";
Cout() << iv2.ToString() << "\n";
}
I expect iv2 to be equal to iv at the end. There is probably something wrong with the deep copy constructor but I really can't figure it out.
Victor
[Updated on: Wed, 07 March 2007 00:55] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: Derivating from Vector<> [message #8406 is a reply to message #8402] |
Wed, 07 March 2007 17:05 |
victorb
Messages: 78 Registered: December 2005 Location: Nice, France
|
Member |
|
|
Simpler, without having to use multiple inheritance:
class IntVector : public DeepCopyOption<IntVector, Vector<int> >
{
public:
IntVector(){Cout() << "IntVector\n"; }
virtual ~IntVector(){Cout() << "~IntVector\n";}
IntVector(const IntVector &src, int)
{
Append(src);
name = src.name;
}
String name;
};
Should be the ultimate solution
[Updated on: Wed, 07 March 2007 17:06] Report message to a moderator
|
|
|
|
Re: Derivating from Vector<> [message #8422 is a reply to message #8415] |
Thu, 08 March 2007 16:30 |
victorb
Messages: 78 Registered: December 2005 Location: Nice, France
|
Member |
|
|
The main reasons because derivating from containers seem to be:
1- the lack of virtual destructor,
2- member function are not virtual then you can override them.
However in my case I am just adding a few properties to Vector<> and I don't want to have to rewrite the Add()/Remove()/... then I'll stick with inheritance. I agree that composition should be the preferred way in more complex cases.
Thanks,
Victor
|
|
|
Goto Forum:
Current Time: Thu May 16 18:46:05 CEST 2024
Total time taken to generate the page: 0.01527 seconds
|