GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Dec-2005, 18:21
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

Setting a pointer member thru a function


Hi! I have a class that has a pointer member (elem1). I was wondering if I could use a function returning a reference (El below) to change the pointer to point to some other Element object?

CPP / C++ / C Code:

class A {
  private: 
    Element *elem1;
  public:
    Element & El() {return elem1;}
};


Thanks, Michelle
  #2  
Old 01-Dec-2005, 18:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Setting a pointer member thru a function


Could you give a fuller description of what you want to do? I may be just me, but I only see half a description here.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #3  
Old 01-Dec-2005, 18:43
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

Re: Setting a pointer member thru a function


Hi Walt!

This is what I want to do:

CPP / C++ / C Code:

class A {
  private: 
    Element *elem1;
  public:
    A (Element *elem):elem1(elem){};
    Element & El() {return elem1;}
};


main program would be:

CPP / C++ / C Code:
.
.
.
  Element myelement;
  Element yourelement;

  A a(&myelementptr);

  // Can I do this here to assign address of yourelement to pointer elem1
  // of object a?
  a.El() = &yourelement;


Thanks, Michelle
  #4  
Old 01-Dec-2005, 18:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Setting a pointer member thru a function


That's beyond my C++ knowledge... Paramesh? I know you're on line..... and probably answering already
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #5  
Old 01-Dec-2005, 20:33
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 923
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Setting a pointer member thru a function


Yes!
You can do that.

With just a little bit change:
For example, consider that the element is int.
Here is a sample code:
CPP / C++ / C Code:
#include<iostream>
using namespace std;

class A {
  private: 
      int *elem1;                   //elem1 is the pointer to an int

  public:
    A (int *elem):elem1(elem){};    //constructor that takes the pointer to an int

    int* El() {return elem1;}       //returns the pointer elem1

    void print()                    //a sample print function to check this program
    {
        cout<<*elem1;
    }
};


int main()
{

    int x=10;

    A a(&x);        //create object with pointer to x as the constructor
    
    a.print();      //print the value in elem1

    int* z;         //element pointer

    z = a.El();     //NOW!!! We are returning the pointer elem1 to z

    *z = 100;       //We are using z!
        
    a.print();      //Print and see the output!!!!!!!!!!!!!!!!

    return 0;
}


Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #6  
Old 02-Dec-2005, 06:20
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

Re: Setting a pointer member thru a function


Hi Paramesh!

Thanks! That helps a lot!

Michelle
  #7  
Old 02-Dec-2005, 08:30
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

Re: Setting a pointer member thru a function


Hi Paramesh!

I just noticed that that code changes the value of the data pointed by elem1, but not the pointer (it's still the same address in memory). What I want to change is the pointer to point someplace else.

Thanks, Michelle
  #8  
Old 02-Dec-2005, 08:49
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 923
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Setting a pointer member thru a function


For that, add an extra member function like this:
CPP / C++ / C Code:
    void assign(int *x)
    {
        elem1 = x;
    }

Then, in your main function, you can do like this:
CPP / C++ / C Code:
    int x = 100;

    A a(&x);      // elem1 points to x

    int y = 10;

    a.assign(&y);      // change the pointer to y

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 11:33
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.