引用,AddressOf和对象管理 [英] References, AddressOf and Object management

查看:63
本文介绍了引用,AddressOf和对象管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否认为您不能重新分配对不同的

对象的引用?如果是这样,这里会发生什么:


struct A

{

DoSomeThing();

};


A& GetNextA();


而(1)

{

A& a = GetNextA();


a.DoSomeThing();

}


引用''a ''只在循环中第一次被分配,然后

,对象''''被分配给下一个''A''......或者是''a''连接<每次循环时,每个不同的''A'是
?这个怎么样:


而(1)

{

{

A& a = GetNextA();


a.DoSomeThing();

}

}


当然,'a''现在每次循环都会有所不同(因为''a''的
范围在循环之前结束了吗?


在相关的说明中,是否可以阻止对类

实例的引用?同样,是否可以阻止实例的地址

来自正在采取什么?


我正在寻找的是一种实现''参考< T>''课程的方法。在其他

字样中,某种类似智能指针的类除了''引用''

是访问它引用的对象的唯一方法。我想阻止

''address-of''和引用并使用''Reference'作为管理

对象生命周期的手段。


任何见解或非常感谢链接到好文章。


问候

Tim

解决方案



" Tim Clacy" <无******* @ nospamphaseone.nospamdk>在留言新闻中写道:40 ********************* @ dread11.news.tele.dk。 ..

while(1)
{A / amp; a = GetNextA();

aSDoTomeThing();
}

因为a没有被声明为静态,每次代码通过时都会被初始化

块。

while(1)
{
{
A& ; a = GetNextA();

a.DoSomeThing();
}
}


额外的{}没有额外的效果这里。

在相关的说明中,是否可以阻止对类
实例的引用?同样,是否可以防止实例的地址被采取?


你不能做任何事情来防止价值被绑定到参考。

你可以通过重载使得它的地址变得困难一元一元

&班级的操作员,但我不确定这将是一个傻瓜式的证据

完全禁用它的方式。

我正在寻找的是一种方式实现''参考< T>''类。在其他方面,某种类似智能指针的类除了引用之外,它是访问它所引用的对象的唯一方法。我想阻止
''address-of''和引用,并使用''Reference'作为管理对象生命周期的手段。



我不清楚你真正想要完成什么,但是如果你没有b $ b专门提供一种方法,参考< T>不会与T& ;.绑定。

然而这样的课程会有其他问题。具体来说,你不能超载

运营商(期间)。 。


Tim Clacy写道:

我认为你不能重新指定一个引用不同的对象?如果是这样,这里会发生什么:


是的。


[snip] {
A& a = GetNextA();

aSomeThing();
}


对象a仅存在于此范围内。一旦留下范围,它就应该销毁
。当循环重新进入范围时,将创建一个新对象

,也称为a。


我认为编译器优化可能会改变这个某些行为在某种情况下无法察觉。


请注意,我不确定这些是什么。您可以随时尝试并且

看看您的编译器做了什么。

引用''a''仅在循环中第一次被分配,然后
,对象''''被分配到下一个''A''......或者每次在循环中被'a''连接到不同的''A'?那怎么样:

而(1)
{
{A / amp; a = GetNextA();

a.SDoSomeThing();
}
}


叫我疯了,但不是''这与最后一段代码相同吗?你只需要增加一个额外的(多余的)范围就可以了。

当然,a现在每次循环都会有所不同(因为


是的,我很确定它们是相同的。

在相关的说明中,是否有可能阻止对类
实例的引用?同样,是否可以阻止实例的地址被采取?

我在做什么寻找是一种实现''参考< T>''类的方法。用其他的方式来说,某种类似智能指针的类除了''参考''
是唯一的访问它引用的对象的方法。我想阻止
''address-of''和引用,并使用''Reference'作为管理对象生命周期的手段。




我通过在每个类中编写公共静态创建方法来实现这一点

返回一个智能p ointer到新对象。所有构造函数都是私有/受保护的,因此没有办法在静态方法之外创建对象。因此,没有直接访问对象

本身(仅通过智能指针),因此无法获取它的引用或指针

(除非智能指针允许它)。


喜欢这样:


class SafeObject

{

public :

静态MySmartPtr< SafeObject>创建();


私有:

SafeObject(){}


//可能不要需要私人复制或复制分配

//运营商,因为你无法直接访问对象

//无论如何都要使用它们。

};


Where" MySmartPtr"是你的参考类模板。您将能够直接获取MySmartPtr的引用或指针,但不能直接获取SafeObject

(据我所知)。

希望有所帮助。


- Pete


Pete Vidler写道:

Tim Clacy写道:

我认为你不能重新分配对不同对象的引用吗?如果是这样,这里应该发生什么:

[snip]

{
A& a = GetNextA();

aSomeThing();
}



对象a仅存在于此范围内。一旦留下范围,它就应该被销毁。当循环重新进入范围时,将创建一个新的
对象,也称为a,将被创建。




Pete,


嗨。所以当你循环时,这就像离开一个范围然后重新进入?

我认为编译器优化可能会以一些不明显的方式改变这种行为。

注意我不确定这些。您可以随时尝试
并查看编译器的功能。

引用''a''仅在循环中第一次被分配,并且<之后,对象''''被分配给下一个''A''......或者是'a''
每次在循环中连接到不同的''A'?怎么样呢
这个:

而(1)
{
{A / amp; a = GetNextA();

a.DoSomeThing();
}
}
叫我疯了,但与最后一点不一样代码?
你只是添加一个额外的(多余的)范围。

当然,'a''现在每次都会有所不同循环
(因为a的范围在循环之前结束了?



是的,我很确定它们是相同的。




是的,听起来它们是一样的。我不是(仍然没有)完全清楚

关于变量/引用/对象的生命周期一个循环范围。如果

变量/引用/对象被破坏并重新构造每个循环

循环,那么我的第二种情况与第一种情况相同。

在相关的说明中,是否可以阻止对类
实例的引用?同样,是否可以预先设置是否取得了
实例的地址?

我正在寻找的是一种实现''参考< T>''课程的方法。
In换句话说,某种类似智能指针的类除了参考是访问它所引用的对象的唯一方法。我想阻止''address-of''和引用,并使用
''Reference'作为管理对象生命周期的手段。



我这样做通过编写公共静态创建
返回指向新对象的智能指针的每个类中的方法。所有构造函数都是私有/受保护的,因此无法在静态方法之外创建对象。因此,没有直接访问
对象本身(仅通过智能指针),因此无法获得指向它的引用或指针(除非智能指针允许)。
<像这样:

类SafeObject
{
公开:
静态MySmartPtr< SafeObject>创建();

私人:
SafeObject(){}
//可能不需要私人复制或复制分配 //运算符,因为你无法直接访问对象
//无论如何都要使用它们。
};

其中MySmartPtr是你的参考类模板。您将能够直接获取MySmartPtr的引用或指针,但不能直接获取SafeObject
(据我所知)。

希望有所帮助。




嗯,这就是我现在所处的地方......但我们所做的一切都被取代了

控制对象生命周期的问题是控制智能指针生命周期的问题。

- Pete



Am I correct in think that you can''t re-assign a reference to a different
object? If so, what should happen here:

struct A
{
DoSomeThing();
};

A& GetNextA();

while (1)
{
A& a = GetNextA();

a.DoSomeThing();
}

Does reference ''a'' only get assigned first time around the loop and,
thereafter, object ''a'' gets assigned to the next ''A''... or is ''a'' connected
to a different ''A'' every time around the loop? What about this:

while (1)
{
{
A& a = GetNextA();

a.DoSomeThing();
}
}

Surely, ''a'' would now be different every time around the loop (because the
scope of ''a'' ends before looping?

On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an instance
from being taken?

What I''m looking for is a way to implement a ''Reference<T>'' class. In other
words, some kind of class like a smart-pointer except that the ''Reference''
is the only way to access the object to which it refers. I want to prevent
''address-of'' and references and use the ''Reference'' as a means to manage
object life-time.

Any insight or links to good articles will be very much appreciated.

Regards
Tim

解决方案


"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote in message news:40*********************@dread11.news.tele.dk. ..

while (1)
{
A& a = GetNextA();

a.DoSomeThing();
}

Since "a" is not declared static, it gets initalized everytime code passes through
the block.
while (1)
{
{
A& a = GetNextA();

a.DoSomeThing();
}
}
The extra { } have no additional effect here.
On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an instance
from being taken?
You can''t do anything to prevent a value from being bound to a reference.
You can make taking the address of it difficult, by overloading the unary
& operator for the class, but I''m not certain that would be a fool proof
way of disabling it entirely.

What I''m looking for is a way to implement a ''Reference<T>'' class. In other
words, some kind of class like a smart-pointer except that the ''Reference''
is the only way to access the object to which it refers. I want to prevent
''address-of'' and references and use the ''Reference'' as a means to manage
object life-time.



I''m unclear of what you''re really trying to accomplish, however provided you don''t
specifically provide a way to do with it, a Reference<T> wouldn''t be bindable to T&.
However such a class would have other problems. Specifically, you can''t overload
operator (period). .


Tim Clacy wrote:

Am I correct in think that you can''t re-assign a reference to a different
object? If so, what should happen here:
Yes.

[snip] {
A& a = GetNextA();

a.DoSomeThing();
}
The object "a" only exists within this scope. Once the scope is left it
should be destroyed. When the loop re-enters the scope a new object,
also called "a", will be created.

I suppose compiler optimisations might change this behaviour in some
unnoticeable way.

Note that I''m not certain about any of this. You could always try it and
see what your compiler does.
Does reference ''a'' only get assigned first time around the loop and,
thereafter, object ''a'' gets assigned to the next ''A''... or is ''a'' connected
to a different ''A'' every time around the loop? What about this:

while (1)
{
{
A& a = GetNextA();

a.DoSomeThing();
}
}
Call me crazy, but isn''t that identical to the last bit of code? You''re
just adding an extra (redundant) scope.
Surely, ''a'' would now be different every time around the loop (because the
scope of ''a'' ends before looping?
Yes, I''m pretty sure they are the same.
On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an instance
from being taken?

What I''m looking for is a way to implement a ''Reference<T>'' class. In other
words, some kind of class like a smart-pointer except that the ''Reference''
is the only way to access the object to which it refers. I want to prevent
''address-of'' and references and use the ''Reference'' as a means to manage
object life-time.



I do this by writing public static "Create" methods in each class that
return a smart pointer to the new object. All constructors are
private/protected so there is no way of creating the object outside of
the static methods. Consequently there is no direct access to the object
itself (only through the smart pointer), so a reference or pointer to it
cannot be obtained (unless the smart pointer allows it).

Like this:

class SafeObject
{
public:
static MySmartPtr< SafeObject > Create();

private:
SafeObject() {}

// Probably don''t need private copy-ctors or copy-assignment
// operators, because you can''t access the object directly
// to use them with anyway.
};

Where "MySmartPtr" is your "Reference" class template. You will be able
to take a reference or pointer of MySmartPtr, but not of SafeObject
directly (to the best of my knowledge).

Hope that helps.

-- Pete


Pete Vidler wrote:

Tim Clacy wrote:

Am I correct in think that you can''t re-assign a reference to a
different object? If so, what should happen here:
Yes.

[snip]

{
A& a = GetNextA();

a.DoSomeThing();
}



The object "a" only exists within this scope. Once the scope is left
it should be destroyed. When the loop re-enters the scope a new
object,
also called "a", will be created.



Pete,

Hi. So when you loop, this is like leaving a scope then re-entering?
I suppose compiler optimisations might change this behaviour in some
unnoticeable way.

Note that I''m not certain about any of this. You could always try it
and
see what your compiler does.

Does reference ''a'' only get assigned first time around the loop and,
thereafter, object ''a'' gets assigned to the next ''A''... or is ''a''
connected to a different ''A'' every time around the loop? What about
this:

while (1)
{
{
A& a = GetNextA();

a.DoSomeThing();
}
}
Call me crazy, but isn''t that identical to the last bit of code?
You''re
just adding an extra (redundant) scope.

Surely, ''a'' would now be different every time around the loop
(because the scope of ''a'' ends before looping?



Yes, I''m pretty sure they are the same.



Yep, it sounds like they are the same. I wasn''t (still not) entirely clear
about life-time of variables/references/objects inside a loop scope. If
variables/references/objects are destroyed and re-constructed every loop
cycle, then my second case is the same as the first.

On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an
instance from being taken?

What I''m looking for is a way to implement a ''Reference<T>'' class.
In other words, some kind of class like a smart-pointer except that
the ''Reference'' is the only way to access the object to which it
refers. I want to prevent ''address-of'' and references and use the
''Reference'' as a means to manage object life-time.



I do this by writing public static "Create" methods in each class that
return a smart pointer to the new object. All constructors are
private/protected so there is no way of creating the object outside of
the static methods. Consequently there is no direct access to the
object itself (only through the smart pointer), so a reference or
pointer to it cannot be obtained (unless the smart pointer allows it).

Like this:

class SafeObject
{
public:
static MySmartPtr< SafeObject > Create();

private:
SafeObject() {}

// Probably don''t need private copy-ctors or copy-assignment
// operators, because you can''t access the object directly
// to use them with anyway.
};

Where "MySmartPtr" is your "Reference" class template. You will be
able
to take a reference or pointer of MySmartPtr, but not of SafeObject
directly (to the best of my knowledge).

Hope that helps.



Hmm, that''s about where I am right now... but all we''ve done is replaced the
problem of controlling the object life-time with a problem of controlling
the smart-pointer life-time.

-- Pete



这篇关于引用,AddressOf和对象管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆