Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Derivating from Vector<>
Derivating from Vector<> [message #8343] Sat, 03 March 2007 20:12 Go to next message
victorb is currently offline  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: Deriving from Vector<> [message #8344 is a reply to message #8343] Sat, 03 March 2007 20:37 Go to previous messageGo to next message
victorb is currently offline  victorb
Messages: 78
Registered: December 2005
Location: Nice, France
Member
I have found something working

	IntVector(const IntVector &src, int) {	
		::new IntVector;	
		for (int i = 0; i < src.GetCount(); i++)
			At(i) = src[i];		
		name = src.name;
		Cout() << "DCC\n";				
	}


I would need to add some check in order to make sure that src is not picked...
But really there should be a nicer solution.
Re: Deriving from Vector<> [message #8345 is a reply to message #8344] Sat, 03 March 2007 20:48 Go to previous messageGo to next message
victorb is currently offline  victorb
Messages: 78
Registered: December 2005
Location: Nice, France
Member
Another working solution:

	IntVector(const IntVector &src, int) {	
		::new IntVector;	
		__DeepCopy(src);
		name = src.name;
		Cout() << "DCC\n";				
	}


But this require to change __DeepCopy access to protected in Vcont.h

Anything better ?
Re: Deriving from Vector<> [message #8346 is a reply to message #8345] Sat, 03 March 2007 22:17 Go to previous messageGo to next message
victorb is currently offline  victorb
Messages: 78
Registered: December 2005
Location: Nice, France
Member
Actually latest does not work (not recursive) Sad
Re: Deriving from Vector<> [message #8399 is a reply to message #8346] Wed, 07 March 2007 00:54 Go to previous messageGo to next message
victorb is currently offline  victorb
Messages: 78
Registered: December 2005
Location: Nice, France
Member
I think that the solution is:

class IntVector : public Vector<int>
{
public:
	IntVector(){Cout() << "IntVector\n"; }
	
	virtual ~IntVector(){Cout() <<  "~IntVector\n";}
	
	IntVector(const IntVector &src, int) : Vector<int>(src, 0)
        {
		name = src.name;
	}

	String name;
	
};
Re: Deriving from Vector<> [message #8401 is a reply to message #8399] Wed, 07 March 2007 01:14 Go to previous messageGo to next message
victorb is currently offline  victorb
Messages: 78
Registered: December 2005
Location: Nice, France
Member
not so sure but I'll find...
Re: Derivating from Vector<> [message #8402 is a reply to message #8401] Wed, 07 March 2007 01:30 Go to previous messageGo to next message
victorb is currently offline  victorb
Messages: 78
Registered: December 2005
Location: Nice, France
Member
This seems to do the trick:

class IntVector : public DeepCopyOption<IntVector>, public Vector<int>
{
public:
	IntVector(){Cout() << "IntVector\n"; }
	
	virtual ~IntVector(){Cout() <<  "~IntVector\n";}
	
	IntVector(const IntVector &src, int) : Vector<int>(src, 0)
	{	
		name = src.name;
		Cout() << "DCC\n";
	}

};
Re: Derivating from Vector<> [message #8406 is a reply to message #8402] Wed, 07 March 2007 17:05 Go to previous messageGo to next message
victorb is currently offline  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 Smile

[Updated on: Wed, 07 March 2007 17:06]

Report message to a moderator

Re: Derivating from Vector<> [message #8415 is a reply to message #8406] Thu, 08 March 2007 13:55 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, derivating from container is possible, but generally not quite a good idea. This is true both for U++Core and STL...

Mirek
Re: Derivating from Vector<> [message #8422 is a reply to message #8415] Thu, 08 March 2007 16:30 Go to previous message
victorb is currently offline  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

Previous Topic: something abaut brc file
Next Topic: XML Error
Goto Forum:
  


Current Time: Fri Mar 29 09:58:40 CET 2024

Total time taken to generate the page: 0.02030 seconds