c++classpointers

Three class using each other


Three classes: class_top->class_foo->class_bar;

The class_top creates a instance of class_foo, which will further call class_bar's method. Here is the code. I apologize it is messy, and I think that is probably why I am getting confused.

class_bar.hpp

#ifndef TWOCLASSUSEEACHOTHER_CLASS_BAR_HPP
#define TWOCLASSUSEEACHOTHER_CLASS_BAR_HPP

#include "iostream"

namespace class_bar
{
namespace class_bar_2
{

template <typename T>
class barBase
{
public:
    barBase() = default;
    virtual void bar_print(int n) = 0; // function later gets called to add n to m_k
    void call_bar_print(int n)
    {
        bar_print(n);
    }

private:

};

class bar : public barBase <int>
{
public:
    bar() = default;
    void bar_print(int n);

private:
    int m_k = 1; // m_k initialized as 1
};

}   // class_bar_2
}   // bar

#endif //TWOCLASSUSEEACHOTHER_CLASS_BAR_HPP

class_bar.cpp

#include "../include/class_bar.hpp"
#include <iostream>

namespace class_bar
{
namespace class_bar_2
{

void bar::bar_print(int n)
{
    m_k += n;
    std::cout << "barBase printing m_k = " << m_k<< std::endl;
}

}   // class_bar2
}   // class_bar

class_foo.hpp

#ifndef TWOCLASSUSEEACHOTHER_CLASS_FOO_HPP
#define TWOCLASSUSEEACHOTHER_CLASS_FOO_HPP
#include "stdio.h"

// forward declaration
namespace class_bar
{
namespace class_bar_2
{
class bar;
}
}

using namespace class_bar::class_bar_2;

namespace class_foo
{

class foo
{
public:
    explicit foo(double dt, bar& b, int n=5, bool flag=false);
    void foo_print_from_bar(int n);

private:
    bar* m_b = NULL; // member point of class foo that points to class bar
    double m_dt;
    int m_int;
    bool m_flag;
};
}

#endif //TWOCLASSUSEEACHOTHER_CLASS_FOO_HPP

class_foo.cpp

#include "../include/class_foo.hpp"
#include "../include/class_bar.hpp"

#include <iostream>

namespace class_foo
{
foo::foo(double dt, bar& b, int n, bool flag)
        : m_dt(dt),
          m_b(&b)
{
    m_int = n;
}

void foo::foo_print_from_bar(int n)
{
    std::cout << m_dt << m_b <<m_int << m_flag;
    std::cout << "foo print using ";
    m_b->call_bar_print(n); // method of class barBase to do addition and pinting
}
}

class_top.hpp

#ifndef THREECLASSUSEEACHOTHER_CLASS_TOP_HPP
#define THREECLASSUSEEACHOTHER_CLASS_TOP_HPP
#include "class_bar.hpp"
#include "class_foo.hpp"
#include "vector"

using namespace class_bar::class_bar_2;

class top
{
public:
    top();
    void top_print(int n);
private:
    class_foo::foo m_foo;
};

#endif //THREECLASSUSEEACHOTHER_CLASS_TOP_HPP

class_top.cpp

#include "../include/class_top.hpp"

using namespace class_bar::class_bar_2;
using namespace class_foo;

void top::top_print(int n)
{
    bar b;
    foo f(3, b);
    m_foo = f;  // line where problem occurs
    m_foo.foo_print_from_bar(n);
}

main.cpp

#include "include/class_top.hpp"

int main()
{
    top* t;
    t->top_print(102); // foo calls bar to print

    return 0;
}

Not all the code here is useful, so I apologize again.

The code compiled successfully, but when running it, the line throws: Process finished with exit code 139 (interrupted by signal 11:SIGSEGV);

When I am debugging it, it seems following line is where the problem occursL

 m_foo = f;  // line where problem occurs

in class_top.cpp. Apparently this is not how m_foo should be used.

Trying to find how to solve this issue, so any pointer is appreciated. Some code demo would be really nice.


Solution

  • The root of your error is that in main(), this code invokes Undefined Behavior at runtime:

    top* t;
    t->top_print(102);
    

    You are calling top_print() on an invalid top object via an uninitialized pointer. Which means top_print() will try to access an m_foo object which does not exist in memory.

    Use this instead:

    top t;
    t.top_print(102);
    

    That creates an actual top object, and then calls top_print() on it.


    That being said, there are a number of other problems with your code, as well:

    With all of that said, here is an online demo that addresses all of the problems mentioned above:

    https://onlinegdb.com/8cwnxE1uLs