7 Replies - 382 Views - Last Post: 19 December 2011 - 08:29 PM Rate Topic: -----

#1 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 05:22 PM

Okay so I keep running into a problem when I program. In my current code, I am making a binary tree. I want to make a function that will print a certain node in the tree. For example if I have a node * temp and it points to something I want to be able to do temp->printMe(). I know I can do temp->printMe(temp) and send the memory address as a parameter but I have run into this problem too often and want to know if somehow the memory address can be accessed with it be sent.
Appreciate a reply. THANKS IN ADVANCE

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing the memory address of the object in a class function...

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 07:12 PM

Whenever you call a member function of a class you can use the this variable that represents the object calling the function.
class A {
    int a;
    void setA (int a) {
        this->a = a;
    }
};


This post has been edited by GWatt: 19 December 2011 - 07:12 PM

Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 07:14 PM

it would be even better if we could see the exact issue you were having. would you mind posting the code you were having issues with and any errors you were getting?
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 07:18 PM

temp->printMe(temp) doesn't really make sense. Every object (every instance of a class) already knows its own address, so there's no reason for a method to require that object (or it's address) to be supplied as a parameter. The signature of printMe should really be
void printMe()


As to this

Quote

want to know if somehow the memory address can be accessed with it be sent.

I don't understand what you meant. Can you clarify your question?
Was This Post Helpful? 1
  • +
  • -

#5 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

Re: Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 07:26 PM

View Postishkabible, on 19 December 2011 - 07:14 PM, said:

it would be even better if we could see the exact issue you were having. would you mind posting the code you were having issues with and any errors you were getting?


Below is my current code. It is for a binary Tree. When I am calling tree.prepNode at the end of the while loop in main, I am having to send the address of tree. Is there a way to access the memory of tree without sending it via the parameter INSIDE the class function. Also I am now coming across a really weird error and have tried for hours and failed to isolate the error. I know it happens in the when prepNode is called. The compiler gives me the follow error:

[Switching to process 737]
Running…
Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb) 



It points to RecType struct as being the source of the error which honestly puzzles me. Can you please also look into this?

 
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct RecType {
  long id;
  string firstName;
  string lastName;
  double amount;
};

class leaf;
typedef leaf * leafPtr;

class leaf{
  leafPtr head;
public:
  leaf() {
    head = NULL;
    left = NULL;
    right = NULL;
  }
  RecType rec;
  leafPtr left;
  leafPtr right;
  void print(leafPtr me);
  void insert(leafPtr tryIns, leafPtr newLeaf,leafPtr parentPtr);
  void prepNode(RecType newRec, leafPtr tree);
};


void leaf::print(leafPtr me){
  string name = me->rec.firstName + me->rec.lastName;
  cout.width(25); cout << internal << me->rec.id << endl;
  cout << "xxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
  cout << "x"; cout.width(23); cout<< internal << name << "x" << endl;
  cout << "x"; cout.width(23); cout<< internal << me->rec.amount << "x" << endl;
  cout << "xxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
}

void leaf::prepNode(RecType account, leafPtr tree){
  leafPtr newLeaf;
  newLeaf->rec = account;
  tree->insert(tree, newLeaf, tree);
}

void leaf::insert(leafPtr tryIns, leafPtr newLeaf,leafPtr parentPtr){
  if(tryIns == NULL){
    newLeaf->head = parentPtr;
    tryIns = newLeaf;
  }
    else if(parentPtr->rec.amount > newLeaf->rec.amount)
      insert(tryIns->left, newLeaf, tryIns);
    else if(parentPtr->rec.amount <= newLeaf->rec.amount)
      insert(tryIns->right, newLeaf, tryIns);
}
  







int main(){
  leaf tree;// LIST
  ifstream fin;
  int i = 1;
  fin.open("Master.txt");
  while(fin.good()){
    long id; 
    string fname, lname;
    double amt;
    fin >> id;
    fin >> fname;
    fin >> lname;
    fin >> amt;
    RecType newOne = { id, fname,lname,amt };
    tree.prepNode(newOne, &tree);
    cout << i << endl;
    i++;
  }
  return 0;
}







View Postr.stiltskin, on 19 December 2011 - 07:18 PM, said:

temp->printMe(temp) doesn't really make sense. Every object (every instance of a class) already knows its own address, so there's no reason for a method to require that object (or it's address) to be supplied as a parameter. The signature of printMe should really be
void printMe()


As to this

Quote

want to know if somehow the memory address can be accessed with it be sent.

I don't understand what you meant. Can you clarify your question?


Yes I want to know how an instance of an object can have access to its own address when you are inside a class function.

This post has been edited by ssharma286: 19 December 2011 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 07:50 PM

The implicit variable this is a pointer to the calling object. In other words it is the memory address. Read my previous post.
Was This Post Helpful? 1
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 08:03 PM

I think your difficulty stems from trying to implement both the tree and the node as a single class. I suggest that you create two classes: tree and node (i.e. your "leaf").

The tree class consists of a node* pointer to the root (which you're calling the "head"), and the methods for insertion, removal, search, print, etc.

The node contains data (or a pointer to data), and node* pointers to its left and right children. Optionally it can have a pointer to the root, which would be set by the tree::insert method.
Was This Post Helpful? 1
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Accessing the memory address of the object in a class function...

Posted 19 December 2011 - 08:29 PM

By the way, the immediate problem is that in prepNode, on line 46 you are trying to assign account data to a nonexistent leaf. newLeaf is just a pointer to which you never allocated any memory.

line 45 should be
leafPtr newLeaf = new leaf;

But really, the whole thing would be much better if you would make the tree and the nodes separate classes.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1