Menu

const_cast problem and Dev Cpp 4.9.7.0

2003-02-15
2012-09-26
  • Nobody/Anonymous

    Hello, I wrote a class that use a copy constructor with a const reference to my class...

    There is a fragment of my code...

    class r_ruota {
            friend ostream& operator << (ostream&, const r_ruota&);
            public:
            r_ruota(const r_ruota& r) {
                    // costruttore copia
                    vector<estratto>::iterator it;
                    no_ruota=r.no_ruota;
                    for (it=const_cast<vector<estratto>::iterator>(r.xresult.begin());
                            it!=const_cast<vector<estratto>::iterator>(r.xresult.end());
                            it++) {
                                    xresult.push_back(*it);
                    }
            }
            r_ruota() : no_ruota(0) { };
            r_ruota(int num) : no_ruota(num) { };
            int no_ruota;
            vector<estratto> xresult;
    };

    I got problems with const_cast and Dev-Cpp 4.9.7.0.
    Dev-C++ 4 compile the source code, also Borland C++ compile it...
    Can anyone help me?

     
    • Nobody/Anonymous

      Why const_cast??

      r isn't changed, is it?

      try without const_cast

       
    • Nobody/Anonymous

      Hello, thank for your answer.
      The use of const_cast is suggested by Bruce Eckel in Thinking C++ vol. 1 at the chapter 3 (The C in C++).
      He said that a const pointer to an object can't be assigned to a nonconst pointer.

      I made your changes and the compiler does, but other compilers (Borland) requires const_cast...

      Thank you.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.