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

大家分析一下这个c++程序问题所在!?

sunny孙 发布于 2013-09-24 15:22, 670 次点击
#include<iostream>
using namespace std;

class Test
{
public:
    int *p;
    Test(int value)
    {
        p =new int(value);
    }
    ~Test()
    {
        delete p;
        p=NULL;
    }
    void PrintValue()
    {
        cout<<"The value is "<<*p<<endl;
    }
};

void Func(Test t)
{
    cout<<"the func"<<endl;
}

int main()
{
    Test t1=33;
    Func(t1);
    t1.PrintValue();
    return 0;
}
编译能通过,结果有问题!
9 回复
#2
sunny孙2013-09-24 16:45
  看了好久,还是自己对指针问题理解不透,问题可能在于int *p定义以后,没有赋初值,也就是没有指向任何地址,在构造函数中直接使用是不行的,也许还有别的问题,希望大家指出来。
#3
zklhp2013-09-24 17:22
程序代码:

#include<iostream>
using namespace std;

class Test
{
public:
    int *p;
    Test(int value)
    {
        p = new int(value);
        cout<<"构造"<<endl;

    }
    ~Test()
    {
        delete p;
        p=NULL;
        cout<<"构造"<<endl;

    }
    void PrintValue()
    {
        cout<<"The value is "<<*p<<endl;
    }
};

void Func(Test t)
{
    cout<<"the func"<<endl;
}

int main()
{
    Test t1(33);
    Func(t1);
    t1.PrintValue();
    return 0;
}


看下这个程序是不是有启发 当然C++我也是业余水平
#4
IT男year2013-09-24 17:53
回复 2楼 sunny孙
这里有对象复制即Func(t1);可能跟没有自定义拷贝构造函数有关系,你自己去看看书吧!我昨天刚好看到拷贝构造函数...只可惜没有看完.明天给你答复哈!
#5
sunny孙2013-09-24 19:09
回复 3楼 zklhp
  这个跟原来的程序没有本质区别吧,输出结果还是错误的,并不是33,您仔细看一下结果。
#6
zklhp2013-09-24 19:38
以下是引用sunny孙在2013-9-24 19:09:56的发言:

  这个跟原来的程序没有本质区别吧,输出结果还是错误的,并不是33,您仔细看一下结果。

程序代码:

#include<iostream>
using namespace std;

class Test
{
public:
    int *p;
    Test(int value)
    {
        p = new int(value);
        cout<<"构造"<<endl;

    }
    ~Test()
    {
        delete p;
        p=NULL;
        cout<<"构造"<<endl;

    }
    void PrintValue()
    {
        cout<<"The value is "<<*p<<endl;
    }
};

void Func(Test t)
{
    cout<<"the func"<<endl;
}

int main()
{
    Test t1(33);
    t1.PrintValue();
    Func(t1);
    return 0;
}


你只是想要结果么 那稍微一改就对了 我想说的是你看看这里构造函数执行了几次。。
#7
sunny孙2013-09-24 21:43
回复 6楼 zklhp
  谢谢指点!
#8
mcm_mingge2013-09-25 10:47
#include<iostream>
using namespace std;

class Test
{
public:
    int *p;
    Test(int value)
    {
        p =new int(value);
    }
    Test(const Test&right)
    {
        p=new int(*right.p);
    }
    ~Test()
    {
        delete p;
        p=NULL;
    }
    void PrintValue()
    {
        cout<<"The value is "<<*p<<endl;
    }
};

void Func(Test t)
{
    cout<<"the func"<<endl;
}

int main()
{
    Test t1=33;
    Func(t1);
    t1.PrintValue();
    return 0;
}
加一个拷贝构造函数。你在调用Func时,t1.p指向的内容被delete掉了。好好看看拷贝构造函数的功能。
#9
IT男year2013-09-25 10:57
八楼正解
#10
IT男year2013-09-25 11:10
#include<iostream>
using namespace std;
class Test
{
public:
     int *p;
     Test(int value)
     {
         p = new int(value);
         cout<<"构造"<<endl;
     }
     Test(const Test & x)
     {
         p=new int(*x.p);
     }
     ~Test()
     {
         delete p;
         p=NULL;
         cout<<"析造"<<endl;
    }
    void PrintValue()
    {
         cout<<"The value is "<<*p<<endl;
    }
};
 
void Func(Test t)
{
     cout<<*t.p<<'\n'<<"the func"<<endl;
}
 
int main()
{
     Test t1(33);
     Func(t1);
     t1.PrintValue();
     return 0;
}
看看我写的这个噢!我说是拷贝构造函数的问题吧!
1