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 » Polymorphic Array doubt
Polymorphic Array doubt [message #33399] Sun, 31 July 2011 23:52 Go to previous message
koldo is currently offline  koldo
Messages: 3361
Registered: August 2008
Senior Veteran
Hello all

I have a doubt with polymorphic Arrays. In my cases methods are called properly but the destructor called is the one of the base class.

For example:

struct Number {
	virtual double Get() const = 0;
};

struct IntegerNumber : public Number {
	int n;
	virtual double Get() const { return n; }

	IntegerNumber(int n) : n(n) {}
};

struct StringNumber : public Number {
	String n;
	virtual double Get() const { return atof(n); }

	StringNumber(String n) : n(n) {}
};


If you include this in your code:

Array<Number> num;
num.Add(new IntegerNumber(3));
num.Add(new StringNumber("15.555555555555555555555555555555555555"));


you will get a "HEAP LEAKS" error when deletion, as for all Array members the Number destructor is called, instead of StringNumber or IntegerNumber.


Best regards
IƱaki

[Updated on: Mon, 01 August 2011 14:45]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: PLATFORM_X11 warnings
Next Topic: BUG? or Not BUG? LoadFile(filename) and then getting wrong data
Goto Forum:
  


Current Time: Wed May 15 04:05:19 CEST 2024

Total time taken to generate the page: 0.02046 seconds