简体   繁体   中英

can not refer to the addresses of constructors

A constructor is a 'special' member function whose task is to initialize the objects of its class.

it is special because its name is the same as class name. the constructor is invoked whenever an
object of its associated class is created.it is called constructor because it constructs the value of data members of class.

A constructor is declared and defined as follows:

//class with a constructor
class integer
{
    int m,n;
public:
    integer(void); // constructor declared
};

integer::integer(void)// constructor defined
{
    m=0;n=0;
}

We can not refer to their addresses but why?

我认为在 C/C++ 中我们有一个函数指针的概念,因为构造函数是一个特殊的成员函数,它们的意思是我们不能像一般函数那样使用函数指针希望它可以帮助你

Because the language doesn't allow any way to take the address of a constructor.

Or, if you're asking why it's not allowed: because there's no reason to do so. You'd only take the address of a function in order to call it, and you never call a constructor directly. It's only called indirectly, as a result of creating an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM