标题: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-242270-1-1.html




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

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