CRITIQUES WELCOME!

Programming, for all ages and all languages.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post 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.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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?
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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:
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post 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.
Author of COBOS
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.)
Every good solution is obvious once you've found it.
Post Reply