I have a class Database. In my class A I create a new object from the database and two objects of class B. Now I want to access the database object from both class B objects. I tried passing the object as pointer and reference but when I print the adress it's always different.
Class A.h:
Database database;
B b1;
B b2;
Class A.cpp:
b1.setDatabase(database);
b2.setDatabase(database);
b1.insert("A");
b2.insert("B");
b1.insert("C");
b2.insert("D");
Class B.h
Database database;
Class B.cpp
void setDatabase(Database& database) {
this->database = database;
}
void insert(std::string name) {
database.dataMap.insert({ name, 10 });
std::cout << database.dataMap.size() << std::endl;
}
Class Database.h
std::map<std::string, int> dataMap;
The output should be 1 2 3 4
But it is
1 1 2 2
So I think it's not the same object
Bneeds to store a pointer or reference toDatabasein order to do this. What does youBlook like?BstoresDatabaseas a copy. So clearly when you modify one of the databases you do nothing to the other or the one you started with.