Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Hi All

Can some explain what are the affects of accessibility of constructor and destructor on scope and the visibility of their class.

Regards
Posted

 
Share this answer
 
Comments
vishal deb 20-Jun-13 13:40pm    
@Richard MacCutchan Thank you for your link
But I need a glass of water and you thrown me in Swimming Pool.

Please give me some easy explanation as I am in learning phase not an expert like you.
JackDingler 20-Jun-13 13:48pm    
Your question is so broad, that you need long winded explanation to answer it.
Sergey Alexandrovich Kryukov 20-Jun-13 18:03pm    
Let me explain you what happened in fact.

You jumped in a pool of water and started to sink. It brought you to the question you are starting to shout to the people who you want to save your: "Hey, help me! Tell me how a water molecule works". :-)

Not really helpful. You should really step back and start learning programming on some regular basis, starting from the basics of OOP, if not from the very beginning. Casual questions on forums cannot help you.

—SA
Richard MacCutchan 21-Jun-13 2:35am    
You need to walk before you can run. Asking a question such as this suggests that you need to go back to the basics of C++ and study it from the beginning. Nothing can change that.
vishal deb 20-Jun-13 13:56pm    
@JackDingler
I have a small theory can you verify my concept -
Accessibility of a class's constructors and destructor only affects the creation or destruction of object of that class. You will need at least one of the publicly accessable constructors or destructor for creating or destructing object of a class.
First, let me say that I am writing this from memory, so there may be errors; check my statements.

If a class has public ctor and dtor, then it can be created anywhere by any code (assuming there are no issues with pure virtual methods etc.).

If a class has a protected constructor, then only a derived class can be instantiated.

If a class has a protected/private destructor, it can be created/deleted as a static object, or on the stack but not on the heap. Or using a factory method.

If a class has a private constructor (and no friends) it can be created using a factory method but not static, stack or heap.

This looks like homework; validate my statements above with some code. I might be wrong...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900