Pointer confusion when returning from method

I have commented the following class with my questions. Basically, I'm just trying to figure out the differences between them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef LISTNODE_H_
#define LISTNODE_H_

template <class Type>

/*
Template for Singly-Linked List, Doubly-Linked List, Circularly-Linked List
and Doubly-Circularly-Linked List
*/
class ListNode
{
private:
	Type data;
	ListNode* next;
	ListNode* previous;
public:
	ListNode(const Type & newData, ListNode * nextPtr = NULL, ListNode * previousPtr = NULL):
	data(newData), next(nextPtr), previous(previousPtr) { };
	/*
	this one seems clear. it returns a pointer to the start of the memory address of data... right?
	*/
	ListNode* getData() { return data };
	/*
	in reference to the 2 methods below,
	are they returning a pointer to the 'next' pointer? or a pointer to what 'next' is pointing to?
	what if it was one of these instead:
	ListNode* getNext() { return *next }; wouldn't this still return a pointer to the value?
	ListNode getNext() { return next }; what am i returning here? can i even do this?
	would either of these make copies instead?
	*/
	ListNode* getNext() { return next };
	ListNode* getPrevious() { return previous };
};

#endif 


Thanks for any help,
Sammie
Hahaha!
You inverted the syntax for getting a pointer and to dereference it!

To get a ListNode *, you should return this->next, since it already is a ListNode *. It's like when you want to get an int, you don't first convert it to double. You just pass it.

To get the object this->next points to, you dereference it: *this->node.
Depending on the class, it may not be possible to make a usable copy of the object.

Don't leave so many (or, in fact, any at all) lines between template and class. They're supposed to be adjacent:
1
2
template <class Type>
class ListNode


getData() says it returns a ListNode *, when data is a Type.
Thanks for the reply helios. And for finding my bug on the return value for Type. I was so focused on the pointer issue I missed that.

Anyway, just to follow-up on your reply:

To get a ListNode *, you should return this->next, since it already is a ListNode *. It's like when you want to get an int, you don't first convert it to double. You just pass it.


Since data is not a pointer, would I need to do this:

1
2
3
4
5
Type* ListNode::getData()
{
  Type* dataPtr = &data;
  return dataPtr;
}
Ok, the snippet above seems to be working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef LISTNODE_H_
#define LISTNODE_H_

template <class Type>
class ListNode
{
private:
	Type data;
	ListNode* next;
	ListNode* previous;
public:
	ListNode(const Type & newData, ListNode * nextPtr = NULL, ListNode * previousPtr = NULL): data(newData), next(nextPtr), previous(previousPtr) { }
	~ListNode() { }
	Type* getData() { return &data; }
	ListNode* getNext() { return next; }
	ListNode* getPrevious() { return previous; }
};

#endif 
Last edited on
Topic archived. No new replies allowed.