I read about structure in c++ that it can not contain instance of itself.
Can anybody help me to understand why it can not contain instance of itself?
I read about structure in c++ that it can not contain instance of itself.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because to create the instance of it, you will need to create the variable, which is itself an instance of it – which will invoke the constructor.
This will result in infinite recursive call to the constructor.
Assume
class Ahas an instance variable nameda:Invoking the constructor of
Awill cause the initialization ofa, which is itself anA. To do it – the constructor ofAwill be invoked again.Note that it will not even compile because the compiler cannot allocate the memory for it, it doesn’t know how much space to allocate for each object. How much space does it take to store the instance variable
a? [Any finite space will not be enough because there will always be an extra variable which also needs to be allocated]