Adding element to vector without knowing the parameter class

Is there a possibility to add an element to the vector without knowing the class of belonging to the object I want to add?

I mean.. if I had a "BaseClass" and two derived classed (Derived1, Derived2)

I have a vector declared like this
1
2
vector<unique_ptr<BaseClass>> vec;


That is a simple attribute..

How can I make this method:

1
2
3
addElement(const BaseClass &object){

}


preserving polymorphism?

For example if I passed a derived object it would save it as a base so, I can't do this:

1
2
3
addElement(const BaseClass &object){
    vec.push_back(make_unique<BaseClass>(object));
}


Even If I write
1
2
3
addElement(const BaseClass &object){
    vec.push_back(make_unique<Derived1>(object));
}

Because if I passed a derived2..

I've tried but didn't work..
Last edited on
1
2
3
void addElement(unique_ptr<BaseClass> object) {
	vec.push_back(move(object));
}
Doesn't work.. i called method passing DerivedClass object but compiler tells me

"No viable conversion from 'DerviedClass' to 'unique_ptr<BaseClass>'"
Does work.

i called method passing DerivedClass object
Don't do that. Pass unique_ptr (by moving it).

addElement(std::move(ptr));

Im so stupid.. i can't understand..

Maybe i'm wrong something..

Actually i have these classes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef Social_hpp
#define Social_hpp

#include <stdio.h>
#include <iostream>
#include "Account.hpp"
#include "User.hpp"
#include "Company.hpp"
#include "Group.hpp"

class Social{
    
private:
    
    vector<unique_ptr<Account>> vec;
    int nElement = 0;
    
    
public:
    Social();
    void addAccount(unique_ptr<Account> object);
    /*
    void Social::addAccount(unique_ptr<Account> object){
    vec.push_back(move(object));
    nElement++;
    }
    */
    void removeElement(const string &username);
    int nOfElement() const;
    void numberPerType(); 
};

#endif 


"Account" is Base class and "User" is my derived class.

In main i did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Account.hpp"
#include "User.hpp"
#include "Social.hpp"

using namespace std;



int main(int argc, const char * argv[]) {
    User u1("vittoc98", "vitto","x", "xxxxx", "21011998");     
     Social *s = new Social();
     s->addAccount(u1);
    
}


U mean that i have to do s->addAccount(move(u1));?
Last edited on
1
2
3
4
5
int main() {
     unique_ptr<User> u1 = make_unique<User>("vittoc98", "vitto","x", "xxxxx", "21011998");
     Social s;
     s.addAccount(move(u1));
}

or, more simply:
1
2
3
4
int main() {
     Social s;
     s.addAccount(make_unique<User>("vittoc98", "vitto","x", "xxxxx", "21011998"));
}
Last edited on
Wow.. thank you man.. really!
If i would split the declaration and then pass the object?
What do you mean with "split the declaration"?
If i would create the object about user and then pass to "addAccount" method.. is it possible?
If i would create the object about user and then pass to "addAccount" method.. is it possible?


That's what has already been shown in this code.
1
2
3
4
5
int main() {
     unique_ptr<User> u1 = make_unique<User>("vittoc98", "vitto","x", "xxxxx", "21011998");  // Create object about user
     Social s;
     s.addAccount(move(u1));  // Pass to addAccount method
}
Note that u1 will be null after it has been passed to addAccount using move. This is because unique_ptr, as the name suggests, tries to ensure that there is only ever one unique_ptr pointing to the same object. This is why copying unique_ptr is not allowed. Instead you have to explicitly allow the object to be transferred from one unique_ptr to another using move.
Last edited on
Last question if u want: what does method move() do on the parameter?

I've read about that but I did not understand why this method allowed me to do it

Why if i do vec.push_back(make_unique<Account>(object))); doesn't work? and with move() work?
Last edited on
Wow.. thank you! i understand all!
Why if i do vec.push_back(make_unique<Account>(object))); doesn't work?


make_unique<Account>(object) is more or less the same as unique_ptr<Account>(new Account(object)).
Topic archived. No new replies allowed.