简体   繁体   English

C ++中内置类型的自定义构造函数

[英]custom constructors for built-in types in c++

Suppose I have defined a class called Entity which has a data member which is a pointer to int. 假设我定义了一个名为Entity的类,该类具有一个数据成员,该数据成员是指向int的指针。 So for example 所以举个例子

class Entity {
public:
    int* ptr;
    // other stuff
};

Is there a way to give the type int* a constructor which takes an Entity object? 有没有一种方法可以给int*类型的构造函数使用Entity对象? If I have an Entity object called my_entity , I'd like to be able to do something like this 如果我有一个名为my_entity的Entity对象,我希望能够做这样的事情

int* p(my_entity);

or this 或这个

int* p = my_entity;

which would require the compiler implicitly call a constructor for int* that takes an Entity . 这将要求编译器隐式调用采用Entity int*的构造函数。

Is this possible? 这可能吗? (I know I could define a public get_pointer() method in the Entity class and do something like int* p = my_entity.get_pointer(); but this seems clunky.) (我知道我可以在Entity类中定义一个公共的get_pointer()方法,并执行类似int* p = my_entity.get_pointer();但这似乎很笨拙。)

Well, there is no constructor for a basic pointer - in the sense that there is no function implicitly called in order to initialise the pointer. 嗯,没有用于基本指针的构造函数-从某种意义上说,没有隐式调用任何函数来初始化指针。

The closest you can come is to use a user-defined conversion operator function 最接近的是使用用户定义的转换运算符函数

class Entity
{
     public:

          operator int *();
};

Entity::operator int *()
{
     // return something appropriate that is of type int *
}


//   sample usage in a function somewhere

int *p = some_entity;    // implicitly conversion that calls the operator int *()

int *another_p = static_cast<int *>(some_entity);    //  explicit conversion

int *yet_another_p = some_entity.operator int *();

There are variants of this, depending on what form of const qualification is needed (eg if the operator function doesn't change the object it acts on, it should be const and may be defined as operator const int *() ). 此方法有多种变体,具体取决于所需的const限定形式(例如,如果运算符函数不更改其作用的对象,则它应该是const并且可以定义为operator const int *() )。

It is necessary to ensure that the pointer returned by the operator function is treated appropriately. 必须确保正确处理了运算符函数返回的指针。 If the user defined operator function returns a member of some_entity , it cannot be used once some_entity ceases to exist. 如果用户定义的运算符函数返回some_entity的成员,则一旦some_entity不存在,将无法使用它。 Similarly, if it uses dynamic memory allocation (eg return the result of a new expression) the caller must explicitly release that memory to avoid a memory leak. 同样,如果它使用动态内存分配(例如,返回new表达式的结果),则调用者必须显式释放该内存,以避免内存泄漏。

You cant make a constructor for primitive types. 您不能为基本类型创建构造函数。 Also there is no need to do this using constructor, you could just use a getter as you already mentioned, or a function to which you pass an Entity and int and store the int to Entity . 也没有必要为此使用构造函数,你可以只使用你已经提到你传递一个一个getter,或功能Entityint ,并存储intEntity

Also you can use a user-defined conversion as @Some programmer dude said and do something like: 您也可以使用@Some程序员dude所说的用户定义的转换 ,并执行以下操作:

int *p = (int*)my_entity;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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