Hello everyone I'm having a little difficulty, hopefully you guys can help or point me to the right direction. I'm trying to get a pointer from a class point to another pointer from another class. Naturally it doesn't work because the assignment operator doesn't allow us to do this so I thought of overloading it.

This is what I have


friend PointerA operator = (const& PointerA * C, const& PointerA *D)

defining the function


PointerA operator = (const& PointerA * C, const& PointerA *D)
{

*A = *B;

[change in post]
what if i // this out? *B = *A;


}

<<*A = *B;
*B = *A;

Looks like you are trying to swap the pointer addresses? Then you need a temporary holding pointer, and that function must return a pointer since that's how you declared it.

PointerA operator = (const& PointerA * C, const& PointerA *D)
{
    PointerA* A = C;
    C = D;
    D = A;
    return C;
}

Yeah, it does seem like that. But I don't know what else to do.
*A should be pointing at *B.

Post some real code you are having problems with so that we know exactly what you are trying to do.

maybe i'm missing something here? but i think perhaps you're making this too hard. *A = *B changes the contents of A to become the same value as the contents of B. A = B changes the pointer address of A, to point instead to the memory location currently pointed to by B.

isn't this second example what you're trying to do?


.

I'll use a struct to illustrate what i'm trying to do.

struct A{
  A *pointer;
}

struct B{
  B *ptr;
}

int main()
{
  A * somePointer = new A();
  B = someptr = new B();

  somePointer = someptr;
}

The compiler says cannot convert 'B' to 'A' in assignment.

That's why I'm overloading the assignment operator.

Are you trying to do this?

#include <iostream>
using namespace std;


struct A{
  A *pointer;
  int data;
};

struct B{
  B *ptr;
  int data2;
};

int main()
{
  A * somePointer = new A();
  B * someptr = new B();
  
  somePointer->data = 5;
  someptr->data2 = 8;

  somePointer = (A*) someptr;
  
  cout << somePointer->data << endl;
  cin.get ();  
  return 0;
}

Output is 8.

I'm trying to have a pointer point to anothier pointer.

>I'm trying to have a pointer point to anothier pointer.
How do you make a pointer point to a non-pointer? Just add a level of indirection:

int x = 12345;
int *px = &x; // Add a level of indirection
int **ppx = &px; // Same thing! Woo-hoo!

std::cout<< x <<' '<< *px <<' '<< **ppx <<'\n';

&x = the address of the number 12345 ?

**ppx is a pointer pointing to *px ?

[additional to post]

But what if when they are pointers from a different class or struct?

you could use a union, in which case either pointer could be used

struct d
{
    union
    {
        struct A* a;
        struct B* b;
    };
};

Or maybe use a base class that is common to both A and B

struct Base
{
    int x;
};

struct A : public Base
{
    int a;
};

struct B : public Base
{
    int b;
};


int main()
{
    struct Base* c;
    struct A* a = new A;
    struct B* b = new B;
    c = a;
}

A slight digression

struct node
   {
       int value;
       node *head;
       node *tail;
   };

   int main()
{
    node *curNode = new node();
    curNode->value = 1;

    curNode->tail= new node();
    curNode->tail->head= curNode;
    curNode = curNode->tail;

If I was to draw this in a picture presentation.

the line node*curNode = new Node();
curNode -> value = 1.

this creates a box that contains the value 1.

then the next line, it creates a new box, and the tail pointer is pointing at it, right?

then the curNode-> tail -> head.

Then to add this to the picture.

the new box tail pointer is pointing to what head pointer is pointing to, which is the first box that was created with the data value 1.

Am I drawning it right?

struct node
   {
       int value;
       node *head;
       node *tail;
   };

   int main()
{
    node *curNode = new node();
    curNode->value = 1;

    curNode->tail= new node();
    curNode->tail->head= curNode;
    curNode = curNode->tail;

new node()???????????
I think you mean new node; there

struct node
   {
       int value;
       node *head;
       node *tail;
   };

   int main()
{
    node *curNode = new node;
    curNode->value = 1;

    curNode->tail= new node;
    curNode->tail->head= curNode;
    curNode = curNode->tail;

You were actually calling a Constructor but not creating a new Node.

so by taking the (), i am in fact making a new node?

kay, but still is my picture accurate?
Or should I take a picture of my picture and post it here?
So that'll be more helpful?

new node()???????????
I think you mean new node; there

Either construct is correct. I don't use () if there are no parameters to pass but I know several people who do.

Ancient Dragon

struct Base
{
    int x;
};

struct A : public Base
{
    int a;
};

struct B : public Base
{
    int b;
};


int main()
{
    struct Base* c;
    struct A* a = new A;
    struct B* b = new B;
    c = a;
}

Does it hae to be like that? Ancient Dragon?
Could it be


struct Base
{
int x;
Base * C;
};

struct A : public Base
{
int y;
A * a;
};


int main()
{
Base aBase;
A aA;

aBase = aA;
}

struct node
   {
       int value;
       node *head;
       node *tail;
   };

   int main()
{
    node *curNode = new node();
    curNode->value = 1;

    curNode->tail= new node;
    curNode->tail->head= curNode;
    curNode = curNode->tail;

Here's the drawning.

Is this right?

http://img90.imageshack.us/img90/329/74307472.jpg

That might work if you make them pointers.

int main()
{
Base* aBase;
A* aA = new A;;

aBase = aA;
}

now somewhere else in your program

void foo(Base* b)
{
   A* a = b;
   // do something with poihnter a
}
int main()
{
    A* aA = new A;
    foo( aA );
 
}

>>Is this right?
I don't know -- that is a mighty strange way to create a linked list.

what would be a more correct way to do it?
Cause Ultimately what I'm trying to do is this.

I have a linked list and I have a separate pointer that's not associated with the linked list. Now when this pointer is NULL, It'll point to the head of the linked list, and does something with it. Then when that's done, it'll point to the new head and does something, repeating the process.

[additonal to the post]

Also, is the picture I drew? Accurate to the code that I tried to draw from the code?

Would the inheritance structs be the solution?

Question.

When deleting a pointer, does the data that pointer points to, is lost?

because it said press x, signaling that you're exiting.
You should recycle the pointer but not the data it holds.
Meaning, reclaim the now unused memory.

>When deleting a pointer, does the data that pointer points to, is lost?
Yes. When you delete a pointer, the only thing you can do with that pointer is change what it points to.

So then how would one access the data that the deleted pointer lost to?

>So then how would one access the data that the deleted pointer lost to?
One wouldn't. Obviously if you still need to access the data then you shouldn't delete the pointer. :icon_rolleyes:

Then what is it meant that, pressing x means exiting/terminating.
Your program should recycle the node, but not the data) in other words reclaim the now unused memory.

Question, say you hae two structs.

struct node{
node* head;
node* next;
};

struct pointer{
  node * pointer;
};

since pointer has a pointer with the data type of Node.
Does that mean, pointer now has a head pointer, and a next pointer? Associated with it? When a pointer from the struct pointer gets created? Since it's datatype is a Node?

I'm thinkin no, since when I created a pointer variable, and have not declare a head variable like the node structer has, it gives me a pointer doesn't have head declared.

Am i doing it wrong? or is it right that you can't do that?

struct pointer{
  node * pointer;
};

I think this is a bad idea as far as naming goes. The name of the struct is the same as the name of a member of the struct. I don't know whether the compiler gets confused, but I definitely do, particularly when you use the word "pointer".

Without the word "new", no node object will be created, so you creating a pointer to a node will not create the node object. Hence head and next will not be created.

Yeah, the naming thing, I was kind of brain dead when I typed that in the post. But in my code, it has another name.

Also, I have seen someone having

new something.

inside a struct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.