首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

类的指针有关问题,请问大大

2012-03-03 
类的指针问题,请教大大classTest{private:char*pcpublic:voidoutText(char*)}Test::Test(){this- pcN

类的指针问题,请教大大
class   Test
{
  private:
    char   *pc;
  public:
    void   outText(char   *);
};

Test::Test()
{
    this-> pc=NULL;
}

void   Test::outText(char   *text)
{
    this-> pc=text;
    for(int   i=0;i <5;i++)
    {
        printf( "%c\n ",this-> pc);
        this-> pc++;
    }
}

-----------------------------------------------

代码如上,类的成员是个char的指针.
在函数outText(char   *)把类成员的指针指向参数.

this-> pc   这句指的是   *pc   还是   pc   的意思阿.?
this-> pc++   是不是等于   pc++   阿?怎么我传进去 "abcde "输出来的东西都是其它字符的.?

我的函数就是实现把传进去的参数每个字符的都赋给pc,然后用pc输出每个字符.
但搞不明白类成员指针的问题.请教大大..

[解决办法]
#include <iostream>
#include <stdlib.h>
using namespace std;

class Test
{
private:
char *pc;
public:
Test();
void outText(char *);
};

Test::Test()
{
this-> pc=NULL;
}

void Test::outText(char *text)
{
this-> pc=text;
for(int i=0;i <5;i++)
{
printf( "%c\n ",*this-> pc);
this-> pc++;
}
}


void main()
{
Test ts;
ts.outText( "abcde ");
system( "PAUSE ");
}

热点排行