注册 登录
编程论坛 C++教室

为什么指针指向类的函数,系统就run不出来了

床至只局 发布于 2015-02-02 12:01, 803 次点击
#include<iostream>
using namespace std;

class cl_1
{
private:
    int a_1,a_2;
public:
    inline void h_1(int,int);
    inline int h_2(void);
    inline int h_3(void);
}op_1,op_2,* op_5,& op_7=op_1;

inline void cl_1::h_1(int b_1,int b_2)
{
    a_1=b_1;
    a_2=b_2;
}

inline int cl_1::h_2(void)
{
    return a_1;
}

void get_1(void){};

inline int cl_1::h_3(void)
{
    return a_2;
}

int main(void)
{
    cl_1 op_3,op_4,* op_6,& op_8=op_2;
    op_1.h_1(1,2);
    cout<<op_1.h_2()<<op_1.h_3()<<"\t1"<<endl;
    op_7.h_1(1,2);
    cout<<op_7.h_2()<<op_7.h_3()<<"\t2"<<endl;
    op_2.h_1(1,2);
    cout<<op_2.h_2()<<op_2.h_3()<<"\t3"<<endl;
    op_3.h_1(1,2);
    cout<<op_3.h_2()<<op_3.h_3()<<"\t4"<<endl;
    op_4.h_1(1,2);
    cout<<op_4.h_2()<<op_4.h_3()<<"\t5"<<endl;
    op_5->h_1(2,3);
    cout<<op_5->h_2()<<op_5->h_3()<<"\t6"<<endl;//??
    op_8.h_1(2,3);
    cout<<op_8.h_2()<<op_8.h_3()<<"\t7"<<endl;
    op_6->h_1(2,3);
    cout<<op_6->h_2()<<op_6->h_3()<<"\t8"<<endl;//??
    return 0;
}
4 回复
#2
rjsp2015-02-02 12:07
代码搞那么复杂干什么
不就是 op_5 是个空指针,op_6 是个未赋值的指针 嘛!
#3
床至只局2015-02-02 12:11
哦哦,谢谢,指针定义时一定要先赋初值,是吧
#4
WebKiller2015-02-04 11:43
不初始化就会有个垃圾值
#5
zklhp2015-02-04 12:00
头像好评
1