| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 855 人关注过本帖
标题:const指针,通不过编译,求助
只看楼主 加入收藏
NordicNavy
Rank: 1
来 自:地球
等 级:新手上路
帖 子:30
专家分:0
注 册:2008-10-20
收藏
 问题点数:0 回复次数:4 
const指针,通不过编译,求助
#include <iostream>
using namespace std;

class Rectangle
{
      public:
               Rectangle();
               ~Rectangle();
               //void SetLength(int length){itsLength = length;}
               //int GetLength() const { return itsLength;}
               void SetWidth(int width){ itsWidth = width;}
               int GetWidth() const { return itsWidth;}
                             
      private:
                //int itsLength;
                int itsWidth;
}
Rectangle::Rectangle()
{
      itsWidth = 5;
                      }
              //itsLength = 6;
              

Rectangle::~Rectangle()
{}
int main()
{
    Rectangle * pRect = new Rectangle;
    const Rectangle * pConstRect = new Rectangle;
    Rectangle * const pConstPtr = new Rectangle;
   
    cout << "pRect width: " << pRect->GetWidth() << "feet\n";
    cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
    cout << "pConstPtr width. " << pConstPtr->GetWidth() << " feet\n";
    pRect -> SetWidth(10);
    pConstRect -> SetWidth(9);
    pConstPtr -> SetWidth(10);
   
    cout << "pRect width: " << pRect->GetWidth() << "feet\n";
    cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
    cout << "pConstPtr width. " << pConstPtr->GetWidth() << " feet\n";
    system("pause");
     }
搜索更多相关主题的帖子: const 指针 编译 
2008-11-04 00:29
newyj
Rank: 2
等 级:新手上路
威 望:3
帖 子:542
专家分:0
注 册:2008-1-4
收藏
得分:0 
首先 类声明后要加 ";"
主函数后要有返回值 return 0;
const Rectangle * pConstRect = new Rectangle; 是指向常量的指针 不能赋值
2008-11-04 11:03
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
pConstRect -> SetWidth(9);
既然是指向常量的指针,那么是不能修改常量的属性的
#include <iostream>
using namespace std;

class Rectangle
{
public:
     Rectangle();
     ~Rectangle();
               //void SetLength(int length){itsLength = length;}
               //int GetLength() const { return itsLength;}
     void SetWidth(int width){ itsWidth = width;}
               int GetWidth() const { return itsWidth;}
                             
      private:
                //int itsLength;
     int itsWidth;
};
Rectangle::Rectangle()
{
      itsWidth = 5;
}
              //itsLength = 6;
              

Rectangle::~Rectangle()
{}
int main()
{
    Rectangle * pRect = new Rectangle;
    const Rectangle * pConstRect = new Rectangle;
    Rectangle * const pConstPtr = new Rectangle;
   
    cout << "pRect width: " << pRect->GetWidth() << "feet\n";
    cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
    cout << "pConstPtr width. " << pConstPtr->GetWidth() << " feet\n";
    pRect -> SetWidth(10);
    //pConstRect -> SetWidth(9);
    pConstPtr -> SetWidth(10);
   
    cout << "pRect width: " << pRect->GetWidth() << "feet\n";
    cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
    cout << "pConstPtr width. " << pConstPtr->GetWidth() << " feet\n";
    system("pause");
    return 0;
}

学习需要安静。。海盗要重新来过。。
2008-11-04 11:44
shmilytong
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2008-10-31
收藏
得分:0 
另外
Rectangle * pRect = new Rectangle;应该写成

Rectangle * pRect;
pRect = new Rectangle;
2008-11-04 13:32
newyj
Rank: 2
等 级:新手上路
威 望:3
帖 子:542
专家分:0
注 册:2008-1-4
收藏
得分:0 
ls
这两种 有什么区别吗?
2008-11-04 13:56
快速回复:const指针,通不过编译,求助
数据加载中...
 
   



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

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