来源:http://www.cnblogs.com/clever101
下面的一段代码:
#include"stdafx.h"
#include<iostream>
classPoint3d
{
public:
Point3d(floatx=0.0,floaty=0.0,floatz=0.0)
:_x(x),_y(y),_z(z)
{
}
floatGetX(){return_x;}
floatGetY(){return_y;}
floatGetZ(){return_z;}
inlinestd::ostream&operator<<(std::ostream&out)const
{
out<<GetX()<<""<<GetY()<<""<<GetZ()<<std::endl;
returnout;
}
private:
float_x,_y,_z;
};
VS C++2005编译出现下面错误:
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662:“Point3d::GetX”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
1>转换丢失限定符
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662:“Point3d::GetY”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
1>转换丢失限定符
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662:“Point3d::GetZ”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
原因在于带const修饰符的接口会把this指针转化为为const this类型。网上一种解决办法是,把需要调用的非const接口都改为const,如上例的GetX、GetY和GetZ函数改为:
floatGetX()const{return_x;}
floatGetY()const{return_y;}
floatGetZ()const{return_z;}
我想到的一种办法是可以直接在内部将const修饰符去掉,具体如下:
out<<(const_cast<Point3d*>(this))->GetX()<<""<<(const_cast<Point3d*>(this))->GetY()<<""<<(const_cast<Point3d*>(this))->GetZ()<<std::endl;
分享到:
相关推荐
c语言之const用法 c语言之const用法 c语言之const用法 c语言之const用法 c语言之const用法
uint8_t const at[] = "AT";uint8_t const csq[] = "AT+CSQ";//获取信号强度。uint8_t const cta[] = "AT+CTA=0";//不休眠uint8_t const cpin[] = "AT+CPIN?";uint8_t ...
class string{ friend int len(string &); friend const string & operator+(const string &s1,const string &s2); friend std::ostream & operator(std::ostream &theStream,const string & str); friend const...
int obj_size() const { return sizeof(*this); } const char* GetData()const { return buffer; } const char* c_str()const { return buffer; } bool operator == (const TinyString& other) const; ...
This separates the implementation from the class definition, while still allowing the implementation to be included where necessary. Another use of -inl.h files is for definitions of function ...
Test const class extends Dx Test Case Source Code for Linux.
bool SetAttribute( const std::wstring& name, const std::wstring& value); 设置属性 CONode CreateSubElement( const std::wstring& name); 创建子元素 bool InsertText( const std::wstring& text); 设置文本 ...
void onError(ErrorCode errorCode); void onProgress(int percent); void onSuccess(); void setLuaCallbacks(const std::string& callbackMap); private: std::string *_versionLoadedCallback = nullptr;...
本文章是关于C语言中const定义常量 。
Public Const EM_CHARFROMPOS = &HD7 Public Const EM_GETLINECOUNT = &HBA Public Const EM_GETLINE = &HC4 Public Const EM_LINEINDEX = &HBB Public Const EM_LINELENGTH = &HC1 Declare Sub RtlMoveOptiMemory ...
/ Warp.h: interface for the CWarp class. // ////////////////////////////////////////////////////////////////////// #include #include #include #include #include struct matrix2 { double c11,c12,...
动态数组类模板Array C++ //数组类模板声明 template<class T> class Array { public: Array(int sz = 50);... void Error(ErrorType error, int badIndex = 0)const; //错误处理函数 }; ................
文档用大量的例子介绍了const在c 中的详细用法,看了以后会大有收获!
Public Const ERROR_SUCCESS = 0& Public Const ERROR_NONE = 0 Public Const ERROR_BADDB = 1 Public Const ERROR_BADKEY = 2 Public Const ERROR_CANTOPEN = 3 Public Const ERROR_CANTREAD = 4 Public Const ...
本文是对C语言CONST如何使用的介绍,感兴趣的朋友可以看看。
+Save const:void +Load ifstream& fin :void <<const>>+ inline Id :int <<const>>+ inline Name :string <<const>>+inline Salary :float <<const>>+...
一个较好的介绍CONST数据处理的函数PPT教程。
个人整理收集的关于关键字const的用法小结
关于C语言中const的用法。以下摘自原文:In my last column, I discussed one of the reasons why the rules by which a compiler can place data into ROM are a bit more complicated in C++ than they are in C. ...
总结了const的各种使用方法,及其作用效果,谢谢观看