标题:求解一个关于结构的问题
只看楼主
苑天尤
Rank: 2
等 级:论坛游民
帖 子:17
专家分:16
注 册:2013-10-12
结帖率:0
已结贴  问题点数:3 回复次数:4 
求解一个关于结构的问题
#include<iostream.h>
class student
{
private:
    struct pimpl;
    pimpl m_pimpl;
public:
    student(char * name,int age):m_pimpl.m_name(name),m_pimpl.m_age(age)
  {}
    void show();
};
struct student::pimpl
{
    char * m_name;
    int m_age;
};
void student::show()
{
    cout<<"姓名 :"<<m_pimpl.m_name<<endl;
    cout<<"年龄 :"<<m_pimpl.m_age<<endl;
}
int main()
{
    student a1("谢谢",24);
        a1.show();
    return 0;
}
总是编辑不出结果  望相助
搜索更多相关主题的帖子: private public 
2013-10-29 09:29
wp231957
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册:2012-10-18
得分:2 
-------总是编辑不出结果
  看不明白这几个字是啥意思

DO IT YOURSELF !
2013-10-29 09:32
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
得分:2 
程序代码:
#include<iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
struct pimpl
{
    char *m_name;
    int m_age;
};
class student
{
private:
    pimpl m_pimpl;
public:
    student(char *name,int age)
    {
        m_pimpl.m_name = new char[strlen(name)];
        memcpy(m_pimpl.m_name,name,strlen(name));
        m_pimpl.m_age = age;
    }
    ~student()
    {
        if (m_pimpl.m_name)
        {
            delete m_pimpl.m_name;
            m_pimpl.m_name = NULL;
        }
    }
    void show();
};
void student::show()
{

 cout<<"姓名 :"<<m_pimpl.m_name<<endl;

 cout<<"年龄 :"<<m_pimpl.m_age<<endl;
}
int main()
{
    char szStr[] = "谢谢";
    student a1(szStr,24);
    a1.show();
    return 0;
}

~~~~~~我的明天我知道~~~。
2013-10-29 09:53
lingyg
Rank: 1
等 级:新手上路
威 望:1
帖 子:18
专家分:2
注 册:2014-7-14
得分:0 
#include <string.h>
#include <stdlib.h>
#include "stdafx.h"
#include "iostream"
using namespace std;

struct pimpl
 {
     char *m_name;
     int m_age;
 };
class student
 {
private:
     pimpl m_pimpl;
public:
     student(char *name,int age)
     {
       m_pimpl.m_name = name;
         //m_pimpl.m_name = new char[strlen(name)];
        // memcpy(m_pimpl.m_name,name,strlen(name));
         m_pimpl.m_age = age;
     }
     ~student()
     {
         if (m_pimpl.m_name)
         {
             delete m_pimpl.m_name;
             m_pimpl.m_name = NULL;
         }
     }
     void show();
 };
void student::show()
 {
cout<<"姓名 :"<<m_pimpl.m_name<<endl;
cout<<"年龄 :"<<m_pimpl.m_age<<endl;
 }
int main()
 {
     char szStr[] = "谢谢";
     student a1(szStr,24);
     a1.show();
     return 0;
 }
3楼的代码稍加修改,如上也可以运行,且输出结果没有出现特殊字符,有什么区别呢?

学无止境
2014-07-28 14:28
lingyg
Rank: 1
等 级:新手上路
威 望:1
帖 子:18
专家分:2
注 册:2014-7-14
得分:0 
#include <string.h>
#include <stdlib.h>
#include "stdafx.h"
#include "iostream"
using namespace std;

struct pimpl
 {
     char *m_name;
     int m_age;
 };
class student
 {
private:
     pimpl m_pimpl;
public:
     student(char *name,int age)
     {
       m_pimpl.m_name = name;
         //m_pimpl.m_name = new char[strlen(name)];
        // memcpy(m_pimpl.m_name,name,strlen(name));
         m_pimpl.m_age = age;
     }
     ~student()
     {
         if (m_pimpl.m_name)
         {
             delete m_pimpl.m_name;
             m_pimpl.m_name = NULL;
         }
     }
     void show();
 };
void student::show()
 {
cout<<"姓名 :"<<m_pimpl.m_name<<endl;
cout<<"年龄 :"<<m_pimpl.m_age<<endl;
 }
int main()
 {
     char szStr[] = "谢谢";
     student a1(szStr,24);
     a1.show();
     return 0;
 }
3楼的代码稍加修改,如上也可以运行,且输出结果没有出现特殊字符,有什么区别呢?

学无止境
2014-07-28 14:28



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




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

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