Hi All

I have a query regarding constructor and destructor.
How accessibility of constructor and destructor affects the scope and the visibility of their class.

If someone give some explaination on this I will be thankful.

Regards

It doesn't. The accessibility of a class's constructors and destructor only affects whether and under what conditions you can create instances of the class (specifically you can only directly create instances of the class if at least one of the constructors is accessible to you and you can only create instances on the stack (or delete instances on the heap) if the destructor is accessible). It does not affect whether or under what conditions you can refer to the class at all (e.g. changing the constructors' or destructor's accessibility won't ever change whether or not you're allowed to access a static member of a class or whether you're allowed to use the class name as an argument to sizeof).

@sepp2K
Thank you for your detail explanation may I get some code snippet for the same

I think the easiest way to understand is to understand where Constructors and Destructors come from. Back in the days of C you did not have classes, only structs. As such you had to write a function that would create your struct (and usually return a pointer to it) as such you can think of Constructors and Destructors as functions that the compiler automatically inserts. Here is an example:

class TestClass;
//We are going to pretend that TestClass's constructor is actually this: (its not too far from the truth)
TestClass TestClassConstructor();
//and that this is its destructor:
void TestClassDestructor(TestClass tc);
//The compiler is going to replace this line:
TestClass myTestObject;
//with this one:
TestClass myTestObject=TestClassConstructor();
//and when it goes out of scope like in this example:
{
    TestClass test;
}
//the compiler will have to call the destructor
{
    TestClass test=TestClassConstructor();
    //out of scope time, must destruct!
    TestClassDEstructor(test);
}

You can see how since the compiler automatically tries to call the constructor and destructor in a very public way it runs into issues if the constructor or destructor are private. This allows you to do some interesting things. Lets say that you have a class to hold a fraction and you only want to allow people to make it with a constructor that specifies its numerator and denominator. You could do it this way:

class Fraction
{// private:
    int numerator,denominator;
    //Since this constructor is private it cannot be used outside the class
    Fraction();
    public:
    Fraction(int num, int den):numerator(num),denominator(den){}
};

//now this will work:
Fraction frac1(7,8);//frac1 = 7/8
//but this will cause a compiler error:
Fraction frac2;//what is in frac2?!

Of course that is a trivial example, but trust me there are real situations in which you want to restrict how a class can be created. (note also that from inside a Fraction member you CAN use the first constructor). Hope this helps.

Great post Labdabeta

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.