help: A C++ template question [modified]
-
I have the following codes (partially copied for illustration) using template class: template struct _BoundingBox { T x0, x1, y0, y1; }; template class CMyObject { public: typedef struct _BoundingBox stBBOX; CMyObject() { Init(); // do class initialization here } CMyObject(const CMyObject &Obj) // copy constructor { // do some data copy here .... _Box = Obj.GetBountingBox(); } virtual ~CMyObject() {} public: stBBOX GetBoundingBox() { return _Box; } protected: stBBOX _Box; }; int main(argc, char* argv[]) { CMyObject obj1 CMyObject obj2(obj1); } After compile (using VC 2005, the compiler gave the following error message: error C2662: 'CMyObject::GetBoundingBox' : cannot convert 'this' pointer from 'const CMyObject' to 'CMyObject &' with [ T=int ] Conversion loses qualifiers However, if I change _Box = Obj.GetBountingBox(); to _Box = Obj._Box; in the copy constructor, the error disappears. Anyone knows what the problem is? Thanks.
modified on Tuesday, February 24, 2009 5:18 PM
-
I have the following codes (partially copied for illustration) using template class: template struct _BoundingBox { T x0, x1, y0, y1; }; template class CMyObject { public: typedef struct _BoundingBox stBBOX; CMyObject() { Init(); // do class initialization here } CMyObject(const CMyObject &Obj) // copy constructor { // do some data copy here .... _Box = Obj.GetBountingBox(); } virtual ~CMyObject() {} public: stBBOX GetBoundingBox() { return _Box; } protected: stBBOX _Box; }; int main(argc, char* argv[]) { CMyObject obj1 CMyObject obj2(obj1); } After compile (using VC 2005, the compiler gave the following error message: error C2662: 'CMyObject::GetBoundingBox' : cannot convert 'this' pointer from 'const CMyObject' to 'CMyObject &' with [ T=int ] Conversion loses qualifiers However, if I change _Box = Obj.GetBountingBox(); to _Box = Obj._Box; in the copy constructor, the error disappears. Anyone knows what the problem is? Thanks.
modified on Tuesday, February 24, 2009 5:18 PM
You need to make
GetBoundingBox
aconst
method ofCMyObject
:stBBOX GetBoundingBox() const { return _Box; }
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
You need to make
GetBoundingBox
aconst
method ofCMyObject
:stBBOX GetBoundingBox() const { return _Box; }
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Thanks. I tried according to your suggestion and it worked. But i still didn't get it why GetBoundingBox() should be a const function? what if the return value is a pointer?
The return value doesn't matter - it's where you use GetBoundingBox:
CMyObject(const CMyObject<t> &Obj) // copy constructor { // do some data copy here .... \_Box = Obj.GetBountingBox(); }
You are saying that Obj is const. Given that, you can only perform const methods on Obj. So, to use GetBoundingBox there, it has to be a const method. A const method is making a promise that it doesn't alter the object on which it's called.
_Box = Obj._Box
is implicitly 'const', as you are reading a member variable (it's an rvalue). The error message gcc gives for your code is a bit better than VC++'s:a.cpp: In copy constructor ‘CMyObject<T>::CMyObject(const CMyObject<T>&) [with T = int]’:
a.cpp:34: instantiated from here
a.cpp:20: error: passing ‘const CMyObject<int>’ as ‘this’ argument of ‘_BoundingBox<T> CMyObject<T>::GetBoundingBox() [with T = int]’ discards qualifiersJava, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p