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

求解一个关于结构的问题

苑天尤 发布于 2013-10-29 09:29, 551 次点击
#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;
}
总是编辑不出结果  望相助
4 回复
#2
wp2319572013-10-29 09:32
-------总是编辑不出结果
  看不明白这几个字是啥意思
#3
xufan2013-10-29 09:53
程序代码:
#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;
}
#4
lingyg2014-07-28 14:28
#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楼的代码稍加修改,如上也可以运行,且输出结果没有出现特殊字符,有什么区别呢?
#5
lingyg2014-07-28 14:28
#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楼的代码稍加修改,如上也可以运行,且输出结果没有出现特殊字符,有什么区别呢?
1