I'm really new to c++ and trying construct a basic LinkedList Node just to basically understand the whole pointer/reference thing.
I'm trying to store a reference to an object being passed to the constructor inside my node Class.
I've also created a copy constructor in order to see when Nodes are being copied (for testing purposes)
Node code bellow:
#include <iostream>
using namespace std;
template<typename T>
struct Node {
Node(const T &d): data(d), next(nullptr), prev(nullptr){
;
cout << "created Node obj" << endl;
};
Node (const Node &node) {
cout << "Copied Node obj" << endl;
}
void setNext(Node *next){ this->next = next; };
void setPrev(Node *prev){ this->prev = prev; };
Node* getNext(){ return next ;};
Node* getPrev(){ return prev ;};
T& getData(){ return data ;};
private:
Node *next;
Node *prev;
T data;
};
and also I have this basic code for testing:
#include <iostream>
#include <vector>
#include "Node.h"
using namespace std;
int main() {
vector <int> v1 {1};
Node< vector<int> > n1{v1}; // (1)
Node < Node< vector<int> > > nn1{n1}; // (2)
Node< vector<int> > & n1ref = nn1.getData();
return 0;
}
so first of all using the debugger after line (1) I've seen the copy constructor of vector class is being used for no apparent reason to me.
so I've also tested with line (2) - and indeed the node inside the node is being copied - can someone please clarify why does the node & vector are being copied although I've passed them as a reference?
btw the output of running the main is:
created Node obj
Copied Node obj
created Node obj
UPDATE first of all guys thanks for the commitment, I'm taking in to consideration the notes about storing a pointer, or even a copy (which could make more sense sometimes as i see now)
but the code above is mostly for the practice of pointers/references, so at least for now I would like to stick to the reference member.
after trying @underscore_d 's suggestion to update the member type to T& I've come with the following results:
firstly the IDEA (CLION) made me update the copy constructor - now it has to update the data member in the initialization list. I'm guessing that its because a reference should be assigned upon creation - can someone approve/clarify this?
so the code is as follows:
template<typename T>
struct Node {
Node(const T & d): data(d), next(nullptr), prev(nullptr){
cout << "created Node obj" << endl;
};
Node (const Node &node): data(node.getData()) {
cout << "Copied Node obj" << endl;
}
void setNext(Node *next){ this->next = next; };
void setPrev(Node *prev){ this->prev = prev; };
Node* getNext() const { return next ;};
Node* getPrev() const { return prev ;};
T& getData() const { return data ;};
private:
Node *next;
Node *prev;
T& data;
};
problem is now compilation fails (with the same main class) with the following error:
error: binding 'const std::vector<int>' to reference of type 'std::vector<int>&' discards qualifiers
which I don' fully understand, any help would be appreciated. and again thanks for the efforts to help - very appreciated.