Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,630 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,066 people online right now. Registration is fast and FREE... Join Now!




Pointers in C++

 
Reply to this topicStart new topic

Pointers in C++, Its About pointers.

jignesh145
post 8 Apr, 2006 - 06:50 AM
Post #1


New D.I.C Head

*
Joined: 8 Apr, 2006
Posts: 1


My Contributions


CODE

#include<iostream.h>
#include<conio.h>

struct link
 {
   int data;
   link* next;
 };

class linklist
 {
   private:
     link* first;

   public:
     linklist()
       {
         first = NULL;
       }
     void addItem(int d);
     void display();
  };

void linklist::addItem(int d)
 {
   link* newlink = new link;
   newlink->data = d;
 newlink->next = first;
   first = newlink;
 }
void linklist::display()
 {
   int temp = 0;
   link* current = first;
   while(current != NULL)
     {
       cout<< endl<<"The Data at node: " << temp << " " <<current->data<<endl;
       current = current->next;
       temp++;
     }
   }

void main()
 {
    linklist l1;   // why not - linklist l1 = new linklist();
    l1.addItem(12);
    l1.addItem(14);
    l1.addItem(15);
    l1.addItem(20);

    l1.display();
    getch();
 }


Question:-----Why i cant create object of linklist using new operator.........linklist l1 = new linklist();

This post has been edited by Dark_Nexus: 9 Apr, 2006 - 09:38 PM
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 8 Apr, 2006 - 08:33 AM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


Though i'm not sure this is the place to have asked this question, it is a simple answer.
when you use the 'new' command it allocates memory, this memory is stored on the heap, thus it is created dynamically. In C/C++ when you do this, you must create a pointer to a memory location, not just declare it (I assume you have done java or some language which allows this)
do this instead:
CODE

linklist* l1 = new linklist();


then to access parts of l1 you must use:
CODE

l1->additem(); //for example

read as l1 points to.
It is all about pointers, even in variable creation.
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 8 Apr, 2006 - 08:34 AM
Post #3


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


because you are not creating a pointer you are creating an object

linkedlist* l1 = new linkedlist; // now you are creating a pointer.

Also this should go in the C++ forums, not introduction.

This post has been edited by Mrafcho001: 8 Apr, 2006 - 08:35 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:56AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month