启动指向另一个类的类成员指针的好习惯?

问题描述 投票:0回答:1

下面程序的重点是看到屏幕上打印出7,因为m_k初始化为1,然后加上6。

class_bar.hpp

#ifndef TWOCLASSUSEEACHOTHER_CLASS_BAR_HPP
#define TWOCLASSUSEEACHOTHER_CLASS_BAR_HPP

#include "iostream"

namespace class_bar
{
namespace class_bar_2
{

class foo;

template <typename T>
class barBase
{
public:
    barBase() = default;
    void barBase_print(int n) // function later gets called to add n to m_k
    {
        m_k += n;
        std::cout << "barBase printing m_k = " << m_k<< std::endl;
    }
private:
    int m_k = 1; // m_k initialized as 1
};

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

private:

};

}   // 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()
{
    std::cout << "bar printing" << 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;

// class foo
namespace class_foo
{

class foo
{
public:
    foo() = default;
    void foo_print_from_bar();

private:
    bar* m_b = NULL; // member point of class foo that points to class bar
};
}

#endif //TWOCLASSUSEEACHOTHER_CLASS_FOO_HPP

class_foo.cpp

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

#include <iostream>

namespace class_foo
{
void foo::foo_print_from_bar()
{
    std::cout << "foo print using ";
    m_b->barBase_print(6); // method of class barBase to do addition and pinting
}
}

main.cpp

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

int main()
{
    class_foo::foo f;

    f.foo_print_from_bar(); // foo calls bar to print

    std::cout << "Hello, World!" << std::endl;
    return 0;
}

运行main.cpp时,弹出错误:

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV)

这是因为

NULL
指针

我的问题是,初始化

m_b/bar
的好习惯是什么,以便
m_k
初始化为 1?显然我不希望有任何内存泄漏,那么智能指针是一个解决方案吗?如果是这样我该如何使用它?一些代码示例将非常感激。

c++ class pointers reference
1个回答
0
投票
当您调用

foo::m_b

 时,
bar
未指向有效的
m_b->barBase_print()
对象,因此崩溃。

您可以将

bar&
引用或
bar*
指针传递给
foo
的构造函数来初始化
m_b
,然后
main()
可以创建一个
bar
实例来构造
f
,例如:

class foo
{
public:
    foo(bar& b); // or: foo(bar* b);
    void foo_print_from_bar();

private:
    bar* m_b = NULL; // member point of class foo that points to class bar
};
foo::foo(bar& b) : m_b(&b) {}
// or:
// foo::foo(bar* b) : m_b(b) {}

void foo::foo_print_from_bar()
{
    std::cout << "foo print using ";
    m_b->barBase_print(6); // method of class barBase to do addition and pinting
}
int main()
{
    class_bar::class_bar_2::bar b;
    class_foo::foo f(b); // or: foo f(&b);

    f.foo_print_from_bar(); // foo calls bar to print

    std::cout << "Hello, World!" << std::endl;
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.