标题:向量容器的问题
取消只看楼主
lindayanglong
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2008-3-9
 问题点数:0 回复次数:0 
向量容器的问题
下面的程序我想输出a1的值该怎么写呢?
#include<vector>
#include<iostream>
using namespace std;
int i=0;
int j=0;
class Cdemo
{
public:
    Cdemo():str(NULL)
    {
        cout<<"constructor:"<<i++<<endl;
    };

    Cdemo(const Cdemo &cd)
    {
        cout<<"copy constructor:"<<i++<<endl;
        this->str=new char[strlen(cd.str)+1];
        strcpy(str,cd.str);
    };

    ~Cdemo()
    {
        if(str)
        {
            cout<<"destructor:"<<j++<<endl;
            delete [] str;
        }
    };

    char *str;
    };

    int main()
    {
        Cdemo d1;
        d1.str=new char[32];
        strcpy(d1.str,"trend micro");
        vector<Cdemo> *a1=new vector<Cdemo>();
        a1->push_back(d1);
    //    delete a1; vector 对象指针能够自动析构
        cout<<d1.str<<endl;
        cout<<a1<<endl;   //只输出a1的指针
    
        return 0;
    }
搜索更多相关主题的帖子: 向量 容器 
2008-09-27 12:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-235471-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.406387 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved