Thread: GCC and Copy Constructor

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    7

    GCC and Copy Constructor

    Have Problems to create a Copy Constructor
    Several Tutorials later i didnt get it.
    im using Dev C++ with gcc. i did it in the school some years ago with VS 6

    should be:

    Code:
    class cX
    {
    private: float n;
    public:
    cX(){n=0.0;};
    float GetN(){return n;};
    cX(const cX& x){n=x->GetN();}; // <- COPY N!!
    };
    ERROR: passing `const cX' as `this' argument of `float cX::GetN()' discards qualifiers

    any suggestions?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you have to tell the compiler that it's ok to call GetN() on a const object.

    Code:
    float GetN() const { return n; }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    GetN should be declared const, i.e.,
    Code:
    class cX
    {
    private:
        float n;
    public:
        cX()
        {
            n = 0.0;
        }
    
        float GetN() const
        {
            return n;
        }
    
        cX(const cX& x)
        {
            n = x->GetN();
        }
    };
    However, note that you do not need to define the copy constructor here as the compiler provided one will suffice. Hence:
    Code:
    class cX
    {
    private:
        float n;
    public:
        cX() : n(0.0) {}
    
        float GetN() const
        {
            return n;
        }
    };
    Notice that I used the default constructor's initialiser list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    nice ill try that - thanks for super fast answer

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    really nice 4 hours searching and reding done in 15mins. THX again elkvis & laserlight RoX

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also please take note of Laserlight's formatting of the code. Yours is horrible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Also please take note of Laserlight's formatting of the code. Yours is horrible.
    I wouldn't call it horrible. I've seen much, much worse. It does; however, need improvement.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I've seen worse code, too, but the point is that even if it is worse than this, it is still horrible, because everything that even more horrible than horrible is still be horrible, too.
    The quality of this code is not something you should see in a project. Hence, I would like to call it horrible, because it needs improvement.
    (Anything more horrible than this is horrible, too, and thus also needs to be fixed.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with copy constructor
    By C_programmer.C in forum C++ Programming
    Replies: 4
    Last Post: 06-07-2011, 07:31 AM
  2. Copy Constructor
    By noobcpp in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2007, 06:29 AM
  3. what is copy constructor?
    By Tonyukuk in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2002, 05:54 PM
  4. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  5. Copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 02:02 PM

Tags for this Thread