我在其中一个应用程序中看到了以下代码:
public class First()
{
private Second _second;
public First()
{
_second = new Second(this);
// Doing some other initialization stuff,
}
}
public class Second
{
public Second(First f)
{
}
}
Run Code Online (Sandbox Code Playgroud)
在First()构造函数中,我们First() 在完全构造之前发送类的引用并不是很糟糕吗?我认为只有在控制逻辑离开构造函数时,对象才会完全构造.
或者这没关系?
一个相当理论的问题......为什么常量引用的行为与常量指针的行为不同,我实际上可以更改它们指向的对象?它们看起来像是另一个简单的变量声明.我为什么要用它们?这是我运行的一个简短示例,它编译并运行时没有错误:
int main (){
int i=0;
int y=1;
int&const icr=i;
icr=y; // Can change the object it is pointing to so it's not like a const pointer...
icr=99; // Can assign another value but the value is not assigned to y...
int x=9;
icr=x;
cout<<"icr: "<<icr<<", y:"<<y<<endl;
}
Run Code Online (Sandbox Code Playgroud) 这是允许通过引用传递数组?
void foo(double& *bar)
Run Code Online (Sandbox Code Playgroud)
似乎我的编译器说没有.为什么?通过引用传递数组的正确方法是什么?还是一个解决方法?我有一个数组参数,我的方法应该修改,然后我应该检索.或者,我可以使这个数组成为一个类成员,它工作正常,但它对我的代码的其他部分有很多缺点(我想避免).
感谢致敬.
可能重复:
const引用是否延长了临时的寿命?
我的编译器没有抱怨为const引用分配临时:
string foo() {
return string("123");
};
int main() {
const string& val = foo();
printf("%s\n", val.c_str());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么?我认为返回的字符串foo是临时的,val可以指向生命周期结束的对象.C++标准是否允许这样做并延长返回对象的生命周期?
我试图通过从它的实例借用一个可变引用来操纵x结构的字段。Foofoo
如果我尝试在原始实例移动后x使用y实例的移动绑定打印字段,它会继续打印未更改的值。foo
下面的简化示例:
struct Foo {
x: i32,
}
fn main() {
let mut foo = Foo { x: 42 };
let x = &mut foo.x;
*x = 13;
let y = foo;
println!("{}", y.x); // -> 42; expected result: 13
}
Run Code Online (Sandbox Code Playgroud)
相反,如果我打印移动的绑定y本身,它会打印更改后的值。
println!("{:?}", y); // -> Foo { x: 13 }
Run Code Online (Sandbox Code Playgroud)
或者,如果我在移动之前x或foo.x 之前打印其他内容,它会按预期打印内容。
println!("{}", x); // -> 13
let y = foo; …Run Code Online (Sandbox Code Playgroud) 如果我运行此代码:
Console.WriteLine( String.Format( "{0}", null ) );
Run Code Online (Sandbox Code Playgroud)
我得到一个,ArgumentNullException但如果我运行此代码:
String str = null;
Console.WriteLine( String.Format( "{0}", str ) );
Run Code Online (Sandbox Code Playgroud)
它运行得很好,输出是一个空字符串.
现在两件看起来相当于我 - 他们都传递了一个空引用String.Format()但行为是不同的.
如何识别不同的行为?
范围如何被消耗的一个例子是:
let coll = 1..10;
for i in coll {
println!("i is {}", &i);
}
println!("coll length is {}", coll.len());
Run Code Online (Sandbox Code Playgroud)
这将失败
error[E0382]: borrow of moved value: `coll`
--> src/main.rs:6:35
|
2 | let coll = 1..10;
| ---- move occurs because `coll` has type `std::ops::Range<i32>`, which does not implement the `Copy` trait
3 | for i in coll {
| ----
| |
| `coll` moved due to this implicit call to `.into_iter()`
| help: consider borrowing to avoid moving …Run Code Online (Sandbox Code Playgroud) 让我们说以下是我网站的DIR结构

现在在index.html我可以简单地参考图像
<img src="./images/logo.png">
Run Code Online (Sandbox Code Playgroud)
但是如果我想sub.html在src中引用相同的图像呢?
在Codeigniter中,get_instance()是一个全局可用的函数,它返回包含所有当前加载的类的Controller超级对象(它返回Controller类实例).我将包含当前的源代码:
get_instance() 定义于 Codeigniter.php
// Load the base controller class
require BASEPATH.'core/Controller.php';
function &get_instance()
{
return CI_Controller::get_instance();
}
Run Code Online (Sandbox Code Playgroud)
并CI_Controller定义于Controller.php
class CI_Controller {
private static $instance;
/**
* Constructor
*/
public function __construct()
{
self::$instance =& $this;
// Assign all the class objects that were instantiated by the
// bootstrap file (CodeIgniter.php) to local class variables
// so that CI can run as one big super object.
foreach (is_loaded() as $var => $class)
{
$this->$var =& …Run Code Online (Sandbox Code Playgroud) 在我的工作地点,我看到这种风格被广泛使用: -
#include <iostream>
using namespace std;
class A
{
public:
A(int& thing) : m_thing(thing) {}
void printit() { cout << m_thing << endl; }
protected:
const int& m_thing; //usually would be more complex object
};
int main(int argc, char* argv[])
{
int myint = 5;
A myA(myint);
myA.printit();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有名称来形容这个成语?我假设它是为了防止复制大型复杂对象的可能大量开销?
这通常是好的做法吗?这种方法有什么缺陷吗?