I am new to C++ and have a question:
Compare following code:
class Node {
public:
int data;
Node* x;
};
and
class Node {
public:
int data;
Node x;
};
I know the second part of code can't pass compile. But I want to know the reason.
Is it related to memory allocation or just syntax regulation?
I would be appreciate if someone can solve my question.
In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation, and creates more readable and maintainable code.
Instantiating a ClassThe new operator also invokes the object constructor. Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.
A class declaration can contain static object of self type, it can also have pointer to self type, but it cannot have a non-static object of self type.
Use the deepcopy function to create an identical instance with a new pointer (as opposed to a shallow copy, which is another variable pointing to the same object).
If you could, then a Node
would contain a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
, which contains a Node
... and so on until the universe is full of Nodes
and implodes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With