GCC and Copy Constructor

This is a discussion on GCC and Copy Constructor within the C++ Programming forums, part of the General Programming Boards category; Have Problems to create a Copy Constructor Several Tutorials later i didnt get it. im using Dev C++ with gcc. ...

  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
    2,590
    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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    23,020
    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
    2,590
    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
    Posts
    23,020
    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, 08:31 AM
  2. Copy Constructor
    By noobcpp in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2007, 07: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, 06:03 PM
  5. Copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 03:02 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21