I'm not quite sure what the difference between passing *d
and d2
to the constructor is:
#include <iostream>
using namespace std;
class Data
{
public:
int number;
};
class Node {
public:
Data data;
Node() {};
Node(Data d) : data(d) {};
};
int main()
{
Data* d = new Data();
Node* n = new Node(*d);
Data d2;
Node* n2 = new Node(d2);
return 0;
}
I can pass *d and d2, but in both scenarios, the data member "data" in the class "Node" is still an object by itself, is that correct? Or is there even a difference between passing an object and a dynamic object?
CodePudding user response:
From Node
's perspective, the constructor receives a Data
object. It doesn't care if this object is dynamically allocated using new
or not.
CodePudding user response:
from node perspective, the constructor of node need an object of type data ,so it only care about data object type, it don't care about its place in memory (static memory or dynamic memory) *d and d2 represent data objects so your code is correct.