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 » Community » Newbie corner » [SOLVED]destroying self from array
[SOLVED]destroying self from array [message #26492] Thu, 06 May 2010 14:22 Go to next message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
Hi all Smile

Class T {
public:
    Master* the_lord;

    T(Master* the_lord) : the_lord(the_lord) {}

    void kill_me() {
        ???;
    }
}

Class Master {
public:
    Array<T> slaves;
};


Please, how can I delete the actual array item from it's own instance without eg. adding an index etc.?

Thank You

[Updated on: Thu, 06 May 2010 14:45]

Report message to a moderator

Re: destroying self from array [message #26494 is a reply to message #26492] Thu, 06 May 2010 14:44 Go to previous messageGo to next message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
the worst thing I experience is after namy hours of searchnig/experimenting is ask on the forum and few minutes later find the solution Evil or Very Mad

I got it other way(the T have an a id, so I addresed Master, traverse the array and removed it.

but if the question is answerable, I would like to know it.

thanks

[Updated on: Thu, 06 May 2010 14:48]

Report message to a moderator

Re: destroying self from array [message #26495 is a reply to message #26494] Thu, 06 May 2010 15:14 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

qwerty wrote on Thu, 06 May 2010 14:44

the worst thing I experience is after namy hours of searchnig/experimenting is ask on the forum and few minutes later find the solution Evil or Very Mad
Yes, almost as bad as when someone answers his own question just when you come up with the answer Very Happy

Anyway, I would use similar approach, but based on the address of slave (didn't know they have id).
#include <Core/Core.h>
using namespace Upp;

class T;

class Master {
public:
	Array<T> slaves;
	void KillSlave(T* slave){
		for(int i = 0; i < slaves.GetCount(); i++){
			if(&(slaves[i])==slave) slaves.Remove(i);
		}
	}
};

class T {
public:
	Master* the_lord;
	T(Master* the_lord) : the_lord(the_lord) {}
	void kill_me() {
		the_lord->KillSlave(this);
	}
};


CONSOLE_APP_MAIN{
	Master m;
	m.slaves.Add(&m);
	m.slaves.Add(&m);
	DUMP(m.slaves.GetCount());
	m.slaves[0].kill_me();
	DUMP(m.slaves.GetCount());
}


But what puzzles me, is why would you need that? I have trouble imagining how you call kill_me() without knowing it's index. Te only reason I could come up with is that you use pointers to the slaves somehow. The cleanest way to do this might be using ArrayIndex. The problem would than have single line solution:
	void kill_me() {
		the_lord->slaves.Remove(the_lord->slaves.Find(*this));
	}


Regards,
Honza
Re: destroying self from array [message #26496 is a reply to message #26495] Thu, 06 May 2010 15:50 Go to previous message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
your solution with address wokrs. very thank you

ad the puzzle Smile - lefclicks adding to topwindow ctrls, righclick is removing them.

ad ArrayIndex - I'll take the time to find if I can softly use this nice solution in project; shame on me, I am not faliliar w/ ArrayIndex and Array real use differences yet.

thank you for your ideas.
Previous Topic: Void pointer to NTL Array and VectorMap
Next Topic: real size of window
Goto Forum:
  


Current Time: Thu Mar 28 21:02:24 CET 2024

Total time taken to generate the page: 0.00842 seconds