views:

230

answers:

3

Hi everybaody,
I'm studing C++ and I can't understand the meaning of the boldface sentence below:

From IBM manual:

The following restrictions apply to constructors and destructors:

  • Constructors and destructors do not have return types nor can they return values.
  • References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.
  • Constructors cannot be declared with the keyword virtual.
  • Constructors and destructors cannot be declared static, const, or volatile.
  • Unions cannot contain class objects that have constructors or destructors.

Could you please provide me an example? Thank you!

+9  A: 

You cannot make a function pointer (or reference) that points to a constructor.

Anders Abel
+21  A: 

The sentence means that you can't take pointer to a constructor or a destructor. Here's an example:

class Sample{
    private: int x;
    public: Sample() { x = 100; };
    public: void* member() { x = 200; };
};

template <class X>
void call_me(Sample s, X function){
    (s.*function)();
};

call_me(s, &Sample::member);   //valid
call_me(s, &Sample::Sample);   //invalid
call_me(s, &Sample::~Sample);  //invalid

The rationale is like this:

  1. Constructor doesn't return anything (although it may be thought of a function that returns an initialized object). What would be the return type of it as a member function?
  2. Constructor is not really a member function, in the sense that it can't be called on an object (like s.member()).
  3. There may be several actual functions created for each constructor and destructor. One constructor may allocate memory, the other may not (but still initializing class members in the same way). One destructor may destroy base subobjects, the other may not. In each ctor/dtor invocation in source code compiler chooses the actual "low-level" ctor/dtor to call; this choice is made at compilation time. It can't be done if you invoke it through a pointer.
    Probably this is what meant by "their addresses cannot be taken".
Pavel Shved
+1  A: 

The first guess would be that you can't create a reference or a pointer to a constructor/destructor. Of course, "a reference or a pointer" in this case (if they were possible) would have reference-to-member or pointer-to-member type, since these member functions are not static. However, this interpretation is a problematic for one reason: in C++ there's no such thing as a reference-to-member.

Basically, the mention of "reference" within this interpretation does not make any sense: you can't have a reference to any non-static member function of a class, regardless of whether it is a constructor/destructor or not. There's simply no such thing in C++.

If the above interpretation is correct (as other answers also suggested), the more meaningful (yet still not prefect) wording would be

  • Pointers cannot be used on constructors and destructors because their addresses cannot be taken.

The mention of "references" in this context makes no sense whatsoever.

AndreyT