Oct 13, 2018 at 5:02pm UTC
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 Oct 13, 2018 at 5:03pm UTC
Oct 13, 2018 at 5:40pm UTC
Doesn't work.. i called method passing DerivedClass object but compiler tells me
"No viable conversion from 'DerviedClass' to 'unique_ptr<BaseClass>'"
Oct 13, 2018 at 5:58pm UTC
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 Oct 13, 2018 at 6:00pm UTC
Oct 13, 2018 at 6:03pm UTC
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 Oct 13, 2018 at 6:04pm UTC
Oct 13, 2018 at 6:04pm UTC
Wow.. thank you man.. really!
If i would split the declaration and then pass the object?
Oct 13, 2018 at 6:06pm UTC
What do you mean with "split the declaration"?
Oct 13, 2018 at 6:07pm UTC
If i would create the object about user and then pass to "addAccount" method.. is it possible?
Oct 13, 2018 at 6:28pm UTC
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 Oct 13, 2018 at 6:29pm UTC
Oct 13, 2018 at 6:30pm UTC
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 Oct 13, 2018 at 6:33pm UTC
Oct 13, 2018 at 6:34pm UTC
Wow.. thank you! i understand all!