Thread: what is copy constructor?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    what is copy constructor?

    and how can I use it in a program. For example I want to assign two strings with using a class that I made.

    myclass x("string1,string2");

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    Re: ascii

    eh? Okay, I think I understand. Are you asking about constructor overloading?

    Example
    Code:
    class MyClass {
    protected:
        std::string str1, str2;
    public:
        MyClass(std::string str1, std::string str2) {
            this->str1 = str1;
            this->str2 = str2;
        }
    };

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I think you want to make a constructor that creats an object that is equal to another object, if that's what you want, you should do the following:
    You have to creat a constructor that takes an object of the same class as argument, and make the new object equal to the one given as argument.
    none...

  4. #4
    Shadow12345
    Guest
    im pretty sure that's all a copy constructor is, one that needs the copy of one object to instanciate another. That topic should be covered in a C++ book if you have access to one.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    A copy constructor for a class named myclass would look like the following:

    myclass::myclass(const myclass &mc), m_var1(mc.m_var1), m_var2(mc.m_var2) { /* Other stuff */ };

    myclass x("string1","string2"); is not a copy constructor... it doesn't take an argument of myclass.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor
    By dude543 in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2006, 05:35 PM
  2. illegal copy constructor required?
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 06:27 PM
  3. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM
  4. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 03:04 PM