Keorus
Keorus

Reputation: 9

c++ Object in different place and keeping alive

I'm new to c++ and I'm trying to understand the OOP side of the language and the pointers with (especially smart pointers) in a case for a web caching.

Imagine that I have two classes like this:

template<typename k, typename v>
class A{
 private:
  std::list<std::pair<k, v>> objectList;
  int capacity;
 public:
  void put(k,v); //this function is used to add the values in the objectList
};
class B{
 private:
  string value;
  string text;
 public:
  void setValue();
  string getResult();
};

It is assumed that the implementation of class A and B is written.

My question:

In my source file file of class B, I want to create an instance of my class A in the setValue() function which can be used either everywhere in my function or everywhere in the class but the most important that it is not destroyed with each transaction.

How I could do to create an object A in B, keep it alive and use it in the functions I want from my class B. Using reference or pointer (smart pointer) i think too....

I hope I have succeeded in explaining my problem, thank you for reading.

I tried several things:

1st: I added in my header of class B a variable

A<std::string, std::string>& _v

Then I call it in the source file of class A to call the put() function to add the data I want to it.

2e:

I also tried this:

A<std::string, std::string> _a(capacity);

My concern is that I create a new object each time my method is called, and it is destroyed afterwards.

info: by wandering on several forums and tutorials / courses, I understood that it is necessary to avoid using new

Upvotes: -1

Views: 113

Answers (1)

guivi
guivi

Reputation: 402

I think you have two possible solutions. One is to make the an A ember variable in B. This will give you access to this variable in any member function of B.

This code illustrated the case.

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <list>

template<typename tk, typename tv>
class A{
private:
    std::list<std::pair<tk, tv>> objectList;
    int capacity = 0;
public:
    void put(tk k, tv v)
    {
        objectList.push_back(std::pair<tk, tv>(k, v));
        capacity++;
    }; //this function is used to add the values in the objectList
    void print()
    {
        for (auto a: objectList)
        {
            std::cout << a.first << " " << a.second << std::endl;
        }
    };
};
class B{
private:
    std::string value;
    std::string text;
    A<int, int> m_a;
public:
    void setValue(int a, int b)
    {
        m_a.put(a, b);
        m_a.print();
    };
    std::string getResult()
    {
        return std::string();
    };
};

int main()
{
    B _b;

    _b.setValue(1,1);
    _b.setValue(2,2);
    _b.setValue(3,3);
    return 0;
}

The second is to make A a static variable in the B member function you want to use it. This will not allow you to access A variable from any other member function of B.

This code illustrated the case.

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <list>

template<typename tk, typename tv>
class A{
private:
    std::list<std::pair<tk, tv>> objectList;
    int capacity = 0;
public:
    void put(tk k, tv v)
    {
        objectList.push_back(std::pair<tk, tv>(k, v));
        capacity++;
    }; //this function is used to add the values in the objectList
    void print()
    {
        for (auto a: objectList)
        {
            std::cout << a.first << " " << a.second << std::endl;
        }
    };
};
class B{
private:
    std::string value;
    std::string text;

public:
    void setValue(int a, int b)
    {
        static  A<int, int> _a;
        _a.put(a, b);
        _a.print();
    };
    std::string getResult()
    {
        return std::string();
    };
};

int main()
{
    B _b;

    _b.setValue(1,1);
    _b.setValue(2,2);
    _b.setValue(3,3);
    return 0;
}

You can easily make m_a a pointer if need.

Edit as per what I think the questions in the comment section was asking for:

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <memory>

template<typename tk, typename tv>
class A{
private:
    std::list<std::pair<tk, tv>> objectList;
    int capacity = 0;
public:
    A(){};
    void put(tk& k, tv& v)
    {
        objectList.push_back(std::pair<tk, tv>(k, v)); // this will copy k and v values.
        capacity++;
    }; //this function is used to add the values in the objectList
    void print()
    {
        for (auto a: objectList)
        {
            std::cout << a.first << " " << a.second << std::endl;
        }
    };
};
class B{
private:
    std::string value;
    std::string text;
    std::shared_ptr<A<int, int>> m_a;
public:
    B( std::shared_ptr<A<int, int>> _a)
    {
        m_a = _a;
    };
    void DoSomethingWithA()
    {
        int k =1, v =1;
        m_a->put(k,v);
        k = 2;
        v = 2;
        m_a->put(k,v);
        k = 3;
        v = 3;
        m_a->put(k,v);
    };
};

int main()
{
    std::shared_ptr<A<int, int>> _a = std::make_shared<A<int, int>>();
    std::unique_ptr<B> _b = std::make_unique<B>(_a);
    
    _b->DoSomethingWithA();
    
    _a->print();
    return 0;
}

Upvotes: 2

Related Questions