C ++ the correct way to return a generic type collection

I am new to C ++, and unfortunately, I can’t stop thinking about C # (my old language). I read several books, forums, and a C ++ help site, but I couldn’t find the answer to my question, so I thought that I could try here before giving up and writing something ugly.

Ok, we can start. I have a class with the succesorsFunction abstract method, and I would like it to return a collection of pointers to State . I do not want to force developers to a specific container; I prefer them to choose (vector, list, etc.).

So it looks like this:

class Problem { public: virtual list<const State*>::iterator succesorsFunction(const State &state, list<const State*>::iterator result) const = 0; }; 

the problem here is the explicit use of the list. How do you do this in C ++?

I thought about using templates, but then I ran into two problems: 1) It seems you cannot do this using abstract methods (or am I mistaken?) 2) How can I tell the template that it should contain state pointers?

+6
source share
4 answers

You cannot overload methods based on return types in C ++.

In addition, “containers” in C ++ do not have the same base (for example, Collection in Java), so you cannot return a common container.

I am afraid that there is no pure way to do this.

I would just write overloads (by parameter) or different function names.

For all questions:

1) You can. Why do you think you cannot?

2) Just as you declared list : list<const State*> - const is optional.

+2
source

You cannot have a member function template that is virtual, but you can try to implement a friend generic function as follows:

 template <typename yourType> yourType& succesorsFunction(const State &a, yourType &result){//Your return type can be without reference //Your body return result; } 

If you call your function, for example, with the argument vector<State> a as follows:

 sucessorsFunction(b,a);// b is your State object 

the decontamination process automatically concludes that yourType is actually a vector<State> , which I think solves your problem. In addition, this architecture allows you to create, for example, a new MyVector class (which contains States arrays) and pass the MyVector object to a succesorsFunction .

0
source

If you really want to use the STL container, try the following:

 template <template <typename, typename = std::allocator<const State *> > class Container> Container<const State*> successorsFunction(const State &state, const Container<const State*> &c) const { // return something useful. } 

If you insist that this function be virtual, then it cannot be a member function template, just overload it with the types that you are going to support, then you can make them virtual.

0
source

This is just a clarification in CT's answer. Remember that if you return a container of pointers, you will have to explicitly free them or use std::unique_ptr .
Just FYI .. how are you from C # background.

You can use State or templatize it as well.

 template<typename Type, template< typename, typename = std::allocator<Type*> > class Container > Container<Type*> Successor(const Type& x) { Container<Type*> cont; // something. cont.push_back(new Type(x)); return cont; } 

and name him

 vector<State*> states = Successor<State, vector>(State(10)); 
0
source

Source: https://habr.com/ru/post/909321/


All Articles