C++ question ->please help

  • Wuhoo
  • Newbie
  • Newbie
  • Wuhoo
  • Posts: 5

Post 3+ Months Ago

below is the definition of a c++ linkedlist class.... if you notice in class linkedlist

Listnode *first;
Listnode *last;
int numnodes; are all private members.......

... in the The copy constructor code
->Linkedlist::Linkedlist(const Linkedlist& L)


why is it legal for them to accesss these private vars variables directly???

ie: "numnodes = L.numnodes;
oldnode = L.first;"

I dont understand how that is legal, and ive seen examples like this all over the place....please explain...thx


class Listnode {
public:
Listelement data;
Listnode *next;
};


// The Linked List class
class Linkedlist {
private:
Listnode *first;
Listnode *last;
int numnodes;

public:
Linkedlist(const Linkedlist& L); // Create a LL as a copy of another one

};


// The copy constructor
Linkedlist::Linkedlist(const Linkedlist& L)
{
Listnode *oldnode;
Listnode *newnode;

numnodes = L.numnodes;
oldnode = L.first;
.
.
.
.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

Welcome to OZZU Wuhoo. I'm not certain there's a lot of regulars around here with C++ knowledge, because those posts don't come in too frequently, but I know there's some. Be patient. Just didn't want you to think you were being ignored.
  • Wuhoo
  • Newbie
  • Newbie
  • Wuhoo
  • Posts: 5

Post 3+ Months Ago

another,shorter more to the point, copy constructor example

molecule::molecule(molecule &m)
{
//n is private...
k = m.n
}

^^this works, i see examples everywhere

but if you were in a main function
main{

molecule m;
//once again n is private..
int k = m.n;
}
^^^^^
this is not allowed is it...and if not....what is the difference between the copy constructor and the main example....
  • phpSelectah
  • Student
  • Student
  • User avatar
  • Posts: 97

Post 3+ Months Ago

when in doubt, debug out.

I am pretty sure you will be able to copy private member data into non-OO based variables, but you will never be able to write a private member variable without a native member function.

good luck :D

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 150 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.