Page 2 of 2

Posted: Tue Aug 28, 2007 10:12 am
by eboyd
oh shiza.
i addd the copy ctor for list like you pretty much had osdev64 and then all friggin hell broke loose.

if i let the compiler supply it, it seems fine. im lost.

which brings me to another question:

Do i want object declared like this:

Code: Select all

    dlist<int> list;
    dlist<int> list2 = list;
    dlist<int> list3(list2);
to behave the SAME AS this:

Code: Select all

    dlist<int> *list = new dlist<int>();
    dlist<int> *list2 = list;
    dlist<int> *list3(list2);
???????

It seems that the copy ctor is only envoked for objects like so:

Code: Select all

object myObject;
object another = myObject;
but not for:

Code: Select all

object *myObject = new object();
ojbject *another = myObject;
Somebody pleez explain what the frick is up with all this!?! :-k

Posted: Tue Aug 28, 2007 6:55 pm
by frank

Code: Select all

object myObject;
object another = myObject; 
is assigning the value of an object to an object so a copy would be necessary.

Code: Select all

object *myObject = new object();
ojbject *another = myObject;
is assigning the value of a pointer to a pointer. IE the only thing that should be copied is the address.

Posted: Thu Aug 30, 2007 10:43 am
by eboyd
I'm sorry. I guess I should have clarified my question.

Is the whole point of a copy constructor to STOP copied objects from referencing the same portion of memory?

So if myObj is a list from 1 - 10, and myObj2 is a copy of myObj, if myObj reverses its order, should myObj2 reverse as well?

Posted: Thu Aug 30, 2007 1:53 pm
by eboyd
If what I'm assuming in my previous reply is true, than this is what I've come up with:

Code: Select all

dlist(const dlist& src) : head(new type(T())), tail(new type(T())) {

    clear();
    for(type* temp = (src.head)->next; temp != src.tail; temp =temp->next){
                
        pushb(temp->data);
    }
}
Look good? :wink:

Posted: Sun Sep 02, 2007 2:48 am
by os64dev
Ok, this will problably not hlp much ;-) but if you want to know what happens or need to happen then take a pencil and a piece of paper and draw the objects. trust me it will become very clear then.

Posted: Tue Sep 04, 2007 6:54 am
by Solar
eboyd wrote:Is the whole point of a copy constructor to STOP copied objects from referencing the same portion of memory?
The point of a copy constructor is to do what is "right" for the class in question. If the "right" thing goes beyond a shallow copy, you have to define a copy constructor of your own.
So if myObj is a list from 1 - 10, and myObj2 is a copy of myObj, if myObj reverses its order, should myObj2 reverse as well?
Up to you. ;) I'd say it would be confusing, but your design requirements might be different. (You'd better document it in your headers, though.)