I am making a program with list of classes.
I have this situation that I'm not aware of how to use:

class clsMyClass {
	private:
		unsigned int id, nodStart, nodStop;
		float pipeLen, pipeDiam;
	public:
		void AddElement(clsConducte element);

In another part of program I will have a variable list of this class

list <clsMyClass> pipes;

How can I make this work (with my AddEleement function) so I can write something like this

pipes.AddElement(element);

So how it will the AddElement function be defined and declared so it will work with lists?

I was thinking something like, aldo it does not work:

void clsConducte::AddElement(clsConducte element)
{
	elements.push_back(element);
}

This can't work since elements variable is not defined.
Thanks

Recommended Answers

All 10 Replies

Why do you need AddElement? the list already has functions to add (push_back), delete, insert, etc. All you need to take care of with the clsMyClass is make sure that all constructors work (and do not throw exceptions) and that you have proper copy-semantics, i.e., you need to implement a copy-constructor and assignment-operator if the default copy-semantics are not valid for your case. That's all, enjoy.

I have in one other function that I did not get to yet, with a similar situation where, among other tings, I need to push one element to the front of current's class variable.

It is similar to this but I tried resumeing it to a simplier situation.

Please help me regarding this issue aldo it might seem useless.

Thanks

So if I understand right, you want to do something extra when an element is added to the list (more than just creating the new object and inserting it). Then, you can make a class that contains the list of classes (objects btw) and define your own methods to push, pop, insert, iterate, etc. where you do whatever extra thing you need to do. The point is, that cannot be done within the objects that are held as elements of list.

If you really want to not have a class that manages the list, you can look at what is possible by implementing your own "allocator" (which is the second, "hidden" template parameter to list), but the capabilities are a bit limited, you still won't be able to access the list itself, only the one element you are inserting.

You got it right.
hmm, this is not the answer i was hoping for :(

Thanks for Your help.

Well you can always implement your own linked-list to have all the custom behavior you want. Standard containers like list are "general purpose" meaning they are usually sufficient for most applications, but you can always make your own, but it's a bit more work... or more fun, depending on how you look at it ;) .

Oh, is there a way to pass a second parameter as the current variable.

Something like this

pipes.AddElement(element,pipes);

or like the second element is automatically input (with this pointer or something like)

class clsMyClass {
	private:
		unsigned int id, nodStart, nodStop;
		float pipeLen, pipeDiam;
	public:
		void AddElement(clsConducte element, list<clsMyClass> &elements=this); //or something similar to this that can do the job automatically

And then

void clsConducte::AddElement(clsConducte element, list<clsMyClass> &elements=this)
{
	elements.push_back(element);
}

Well this is possible:

class clsMyClass {
	private:
		unsigned int id, nodStart, nodStop;
		float pipeLen, pipeDiam;
	public:
		void AddElement(clsMyClass element, list<clsMyClass>& elements);
};

//.. in cpp
void clsMyClass::AddElement(clsMyClass element, list<clsMyClass> &elements)
{
        //.. do whatever extra thing you like here.
	elements.push_back(element);
        //.. or here.
}

//main.cpp:

int main() {
  //...
  list <clsMyClass> pipes;
  //...
  pipes.back().AddElement(element,pipes); //see here you have to access back() to call AddElement.
  //watch out that back may not exist if list is empty.
  //...
};

But I would not recommend doing this, it is bad design, too convoluted. Implement your own list class. And if at the lines where I wrote "do whatever extra thing you like here... or here.", you have nothing more to put, then just use list the conventional way:

class clsMyClass {
	private:
		unsigned int id, nodStart, nodStop;
		float pipeLen, pipeDiam;
        public: 
                //.. public interface..
};

//main.cpp:

int main() {
  //...
  list <clsMyClass> pipes;
  pipes.push_back(element); 
  //...
};

Thanks. Aldo I don't understand exacly why I have write the back() since I push_back() and not insert().

pipes.back().AddElement(element,pipes);

well front() maybe.. whatever, I don't understand the purpose of the AddElement function so I don't know how exactly you want to call it. What I do know is that "pipes" is of type "list<clsMyClass>" and that AddElement is a member of clsMyClass, and thus you need to get a clsMyClass object to call AddElement, so "pipes.AddElement(..)" will not compile for sure, but "pipes.back().AddElement(..)" will compile. Whether it does what you want or not I don't know, I am not inside your brain, I can't understand the mean if I don't know the end.

"pipes" is of type "list<clsMyClass>" and that AddElement is a member of clsMyClass, and thus you need to get a clsMyClass object to call AddElement, so "pipes.AddElement(..)" will not compile for sure, but "pipes.back().AddElement(..)" will compile.

Thank You.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.