Query about constructor and destructor

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 explanation on this I will be thankful.

Regards
If the default ctor is private then the class is not default constructable, if the copy ctor is private the class is non-copyable, and if the dtor is private another class must be responsible for destroying it. They are basically pre-c++11 methods of restricting constructing/copying/destructing class objects.
@naraku9333

If the default ctor is private then the class is not default constructable, if the copy ctor is private the class is non-copyable,


I did not find such notion "default constructable" in the C++ Standard. Nevertheless an object of a class can be "default contsructable" even if the default constructor is private. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
 
class A
{
private:
   A() { std::cout << "I'm default constructable" << std::endl; }
public:
   static A create() { return A(); }
};
 
int main()
{
   A a = A::create();
}
Last edited on
The example I showed can be even enhanced the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
 
class A
{
private:
   A() { std::cout << "I'm default constructable" << std::endl; }
public:
   A( const A & ) { std::cout << "Copy constructor" << std::endl; }
   static A create() { return A(); }
};
 
int main()
{
   A a = A::create();
}


Output:

I'm default constructable
Semantics, but I suppose I could have said the private constructor cannot be called outside of the class.
> I did not find such notion "default constructable" in the C++ Standard.

The standard uses the term 'DefaultConstructible'.

Requirements are defined in 17.6.3
(reproduced here: http://en.cppreference.com/w/cpp/concept/DefaultConstructible )

and used in several dozen places in the IS; perhaps the most obvious is in std::is_default_constructible<> http://en.cppreference.com/w/cpp/types/is_default_constructible

The IS makes it unambiguously clear that a type without an accessible explicit or implicit default constructor is not DefaultConstructible.
@JLBorges


For me it is not so clear as for you. Let consider the phrase from the post
If the default ctor is private then the class is not default constructable


Now consider my modified example

1
2
3
4
5
6
7
class A
{
private:
   A() {}
public:
  static A & create() { static A a; return a; }
};


So is the class DefaultConstructible?
Last edited on
the class A is not DefaultConstructible
@Cubbi

Did not I create an instance of the class using the default constructor, did I?
Last edited on
@vlad from moscow


1
2
3
4
5
6
7
8
9
10
11
12
class A
{
private:
   A() {}
public:
  static A & create() { static A a; return a; }
};

int main()
{
    A a ;
}


Using your class can I create an instance of the class with the default constructor? Is the class default constructable to clients of the class? The practical application of the definition should be clear enough.
You already created (defined) an object of the class using the default constructor in static function create.:) It is the answer to your question

can I create an instance of the class with the default constructor?
Last edited on
> So is the class DefaultConstructible?

You can find the answer to that question yourself.
Run this program, first as it is, and then after un-commenting line 13
(ideally with a variety of conforming compilers):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <type_traits>

class A
{
    private:
       A() {}
    public:
      static A & create() { static A a; return a; }

      static void test_it() ;

      // friend class std::is_default_constructible<A> ;
};

void A::test_it()
{
    if( std::is_default_constructible<A>::value )
        std::cout << "the type 'A' is DefaultConstructible\n" ;
    else
        std::cout << "the type 'A' is not DefaultConstructible\n" ;
}


int main()
{
    if( std::is_default_constructible<A>::value )
        std::cout << "the type 'A' is DefaultConstructible\n" ;
    else
        std::cout << "the type 'A' is not DefaultConstructible\n" ;

    A::test_it() ;
}


Ouptput (GCC 4.8)
the type 'A' is not DefaultConstructible
the type 'A' is not DefaultConstructible


the type 'A' is not DefaultConstructible
the type 'A' is not DefaultConstructible
You already created (defined) an object of the class using the default constructor in static function create.:) It is the answer to your question


No, I did not. The static function is part of the class interface. All users of the class know is that create returns a reference to an instance of the class. Users of the class are unable to default construct an object of that class.

A type with a public default constructor is DefaultConstructible.

If no user-defined constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.

Use always public constructor unless:
1.The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category.
2.A utility class, that only contains static methods.

Use of private destructor:
Basically, any time you want some other class to be responsible for the life cycle of your class' objects, or you have reason to prevent the destruction of an object, you can make the destructor private.

Private constructors/destructors prevents inheritance; if any constructors are protected or public, then its still subclassable. For that is better to use:
1
2
3
4
class A final
{
//...
}
I tried this code at www.ideone.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
#include <type_traits>
 
struct A
{
    private:
            A(){}
    public:
            static A & create()
            {
                    static A a;
                    std::cout << std::boolalpha << std::is_default_constructible<A>::value << std::endl;
                    return a;
            }
 
            friend void f( A )
            {
                    std::cout << std::boolalpha << std::is_default_constructible<A>::value << std::endl;
            }
};
 
int main()
{ 
    f( A::create() );
}


and I got compilation errors

usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::__is_default_constructible_impl<A>’:
/usr/include/c++/4.7/type_traits:116:12: required from ‘struct std::__and_<std::__not_<std::is_void<A> >, std::__is_default_constructible_impl<A> >’
/usr/include/c++/4.7/type_traits:682:12: required from ‘struct std::__is_default_constructible_atom<A>’
/usr/include/c++/4.7/type_traits:703:12: required from ‘struct std::__is_default_constructible_safe<A, false>’
/usr/include/c++/4.7/type_traits:709:12: required from ‘struct std::is_default_constructible<A>’
prog.cpp:13:84: required from here
prog.cpp:8:13: error: ‘A::A()’ is private

MS VS 2010 has no is_default_constructible in namespace std.

So could some one compile it in GCC 4.8.
But in any case I think accessibility of a class depends of a context. The definition of defaultconstructibility says nothing about the access control. Sp the realization of std::is_default_constructibility can not satisfies the definition.
> So could some one compile it in GCC 4.8.

Output:
false
false



> But in any case I think ...

It is a free world. You are free to think anything that you please. As long as you do not insist that the thoughts of everyone else must be subservient to your thoughts.

IMHO, no programming language specification can (nor should they be allowed to) impose controls on what an individual thinks or feels. (The situation becomes somewhat tricky if the aforementioned individual ventures to write a compiler with the expectation that it should be used by others to compile their code. Mercifully, that situation does not hold in this case; we are spared further agony.)
Last edited on
@JLBorges


The standard contains many contradictions and even bugs. So it should not be considered as something frozen.

In the table 19 (Table 19 — DefaultConstructible requirements [defaultconstructible]) there is nothing said about the access control. So if an object can be created in some context using the default constructor then why the class is not defaultconstructible?
Last edited on
Topic archived. No new replies allowed.