指针和对象

使用对象指针时,需要注意几点:

 

  • 使用常规表示法来声明指向对象的指针:

                                                       String *glamour;

  • 可以将指针初始化为指向已有的对像:   

                                                      String *first=&saying[0];

  • 可以使用new来初始化指针:

                                                      String *favorite=new String(saying[choice]);

  • 对类使用new将调用相应的类构造函数来初始化新创建的对象:

                                                      String *gleep=new String;//invokes default constructor

                                                      String *glop=new String(“my my my”);//invokes the String(const char *)constructor

                                                      String *favorite=new String(sayings[choice]);

  • 可以使用->操作符通过指针访问类方法:

                                                      if(saying[i].length()<shortest->length())

  • 可以使用对象指针应用解除引用操作来获得对象:

                                                      if(sayings[i]<*first)

                                                                                   first=&sayings[i];

posted @ 2012-08-14 16:53  斗榖於菟  阅读(226)  评论(0编辑  收藏  举报