2

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

5
  • 5
    B needs to store a pointer or reference to Database in order to do this. What does you B look like? Commented Aug 28, 2019 at 12:59
  • 1
    Looking at the code you show your B stores Database as a copy. So clearly when you modify one of the databases you do nothing to the other or the one you started with. Commented Aug 28, 2019 at 13:00
  • I changed the code to store a pointer object. The code compiles but if I run the program it closes immediately. Commented Aug 28, 2019 at 13:04
  • 2
    @MartinM. We'd need a minimal reproducible example in order to help with that. That said, since you already have an answer here, you would need to ask a new question about that. Commented Aug 28, 2019 at 13:05
  • The above code is my full short example. Commented Aug 28, 2019 at 13:09

1 Answer 1

4

You are passing a reference. What you are doing is copying yout Database. You need to copy the address (pointer):

Class B.h

class B
{
private:
  Database* database; //Pointer to Database instead of actual Database object

public:
  void setDatabase(Database* database) //Pass pointer(*) instead of reference(&)
  { 
    this->database = database; // Assign pointer instead of calling the implicit copy constructor
  }

  void insert(string name) { 
    database->dataMap.insert({ name, 10 }); //Dereferencing and member access(->) instead of member access only(.)

    cout << database->dataMap.size() << endl;
  }

}

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");

Live example

Sign up to request clarification or add additional context in comments.

10 Comments

If I change the code to yours I can't insert the values to the map.
You need to use the address operator(&) like this: setDatabase(&database) in your main.
Yes. But I mean the database->dataMap.insert() isn't working
My fault. it's called db now, so 'db->dataMap.insert()' of course.
The db was not the problem. It isn't working anyway.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.