Help with copy constructor

This is a discussion on Help with copy constructor within the C++ Programming forums, part of the General Programming Boards category; I am new to c++ programming. I want to create a program which includes the following line of codes Code: ...

  1. #1
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118

    Help with copy constructor

    I am new to c++ programming.
    I want to create a program which includes the following line of codes
    Code:
    class cars
    {
    .
    .
    .
    };
    class PatrolCars:public cars
    {
    .
    .
    .
    };
    class CNGCars:public cars
    {
    .
    .
    .
    };
    now what the problem is that i want to write a member function which can copy each of the drive class objects to each other I mean these lines should be valid
    Code:
    PatrolCars patcar;
    CNGCars cngcar;
    patcar=cngcar;
    should i create a copy constructor or should i overload an operator for this?
    plz help!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    If they need to be assignable to each other why are they different types? It sounds like only SOME aspects of the objects should be copied. In that case, neither a copy constructor nor an assignment operator should be used, rather something more explicit like CopyCarAspects() or something. Otherwise you have code that does things which look like assignments but are not actually assignments.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    An explicit function like copycarasspects() or something else will require an access to the member data of both types of objects. I dont want to do that.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,669
    It would actually require access to both types' members no matter what method you chose. You could write it in terms of public accessor functions.

  5. #5
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    you mean some thing like this could help

    Code:
    class cars
    {
    .
    .
    .
    };
    class PatrolCars:public cars
    {
    public:
    void PatrolCar (CNGCars c1)
    {
    .
    .
    }
    };
    class CNGCars:public cars
    {
    .
    public:
    void CNGCars(PatrolCars pc1)
    {
    .
    .
    }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor and new
    By Memloop in forum C++ Programming
    Replies: 10
    Last Post: 09-13-2009, 10:23 AM
  2. copy constructor
    By paperbox005 in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2004, 01:43 PM
  3. what is copy constructor?
    By Tonyukuk in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2002, 04: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-30-2002, 02:23 PM

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