读书人

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

发布时间: 2012-03-03 15:33:03 作者: rapoo

类的指针问题,请教大大
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 ");
}

读书人网 >C++

热点推荐