Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. help: A C++ template question [modified]

help: A C++ template question [modified]

Scheduled Pinned Locked Moved ATL / WTL / STL
helpquestionc++
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yellowine
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • Y yellowine

      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

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      You need to make GetBoundingBox a const method of CMyObject:

      stBBOX GetBoundingBox() const { return _Box; }

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      Y 1 Reply Last reply
      0
      • S Stuart Dootson

        You need to make GetBoundingBox a const method of CMyObject:

        stBBOX GetBoundingBox() const { return _Box; }

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        Y Offline
        Y Offline
        yellowine
        wrote on last edited by
        #3

        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?

        S 1 Reply Last reply
        0
        • Y yellowine

          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?

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          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 qualifiers

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups