小编Joh*_*och的帖子

从函数返回const对局部变量的引用

我有一些关于从函数返回对局部变量的引用的问题:

class A {
public:
    A(int xx)
    : x(xx)
    {
        printf("A::A()\n");
    }
};

const A& getA1()
{
    A a(5);
    return a;
}

A& getA2()
{
    A a(5);
    return a;
}

A getA3()
{
    A a(5);
    return a;
}

int main()
{
    const A& newA1 = getA1(); //1
    A& newA2 = getA2(); //2
    A& newA3 = getA3(); //3
}
Run Code Online (Sandbox Code Playgroud)

我的问题是=>

  1. 执行getA1()是否正确?我觉得它是错误的,因为它返回局部变量的地址或临时变量.

  2. main(1,2,3)中的哪些陈述会导致未定义的行为?

  3. const A& newA1 = getA1();做标准的保证,暂时由const引用约束不会被破坏,直到引用超出范围是什么?

c++ reference undefined const-reference

36
推荐指数
2
解决办法
2万
查看次数

如何将BroadCast从一个应用程序发送到另一个应用程序

我有App A和App B.在App中我想向App B发送广播.这是App A的代码:

final Intent intent = new Intent();
intent.setAction("com.pkg.perform.Ruby");
intent.putExtra("KeyName", "code1id");
intent.setComponent(new ComponentName("com.pkg.AppB", "com.pkg.AppB.MainActivity"));
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)

在App B - In中MainActivity,我有MyBroadCastReceiverClass.

public class MainActivity extends Activity {
    private MyBroadcastReceiver MyReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Receive broadcast from External App
        IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby");
        MyReceiver = new MyBroadcastReceiver();
        if(intentFilter != null)
        {
            registerReceiver(MyReceiver, intentFilter);
        }
    }

    public class MyBroadcastReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) { …
Run Code Online (Sandbox Code Playgroud)

android broadcast broadcastreceiver

36
推荐指数
3
解决办法
2万
查看次数

将string_view static_cast为字符串是否合法?

我的问题是由stackoverflow上的这个答案激发的,/sf/answers/3365740731/.报价,

问:你如何转换std::string_viewconst char*

答:只需做一个std::string(string_view_object).c_str()获得保证的以空值终止的临时副本(并在行尾清理它).

不幸的是,它构建了一个新的string.我想知道是否可以做到,

static_cast<string>(string_view_object).c_str()
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是:

  1. 这会构造一个新字符串吗?

  2. 是否保证返回以null结尾的char序列?

我有一小段代码用于演示.它似乎工作正常.(参见wandbox 结果)

#include <string>
#include <iostream>
#include <string_view>
#include <cstring>

int main()
{
  std::string str{"0123456789"};
  std::string_view sv(str.c_str(), 5);

  std::cout << sv << std::endl;
  std::cout << static_cast<std::string>(sv) << std::endl;
  std::cout << strlen(static_cast<std::string>(sv).c_str()) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ static-cast string-view

14
推荐指数
1
解决办法
1037
查看次数

distance_recorder 在 boost 图库中如何工作?

我正在学习呼吸优先搜索算法。该图是一个完全二叉树。我想打印每个节点到根的距离。例如,d[0, 0] = 0, d[0, 1] = 1, d[0, 2] = 1, d[0, 3] = 2, d[0, 7] = 3

在此输入图像描述

点文件在这里。

digraph G {
0;
1;
2;
3;
4;
5;
6;
7;
8;
9;
10;
11;
12;
13;
14;
0->1 ;
0->2 ;
1->3 ;
1->4 ;
2->5 ;
2->6 ;
3->7 ;
3->8 ;
4->9 ;
4->10 ;
5->11 ;
5->12 ;
6->13 ;
6->14 ;
}
Run Code Online (Sandbox Code Playgroud)

程序非常简单,make_bfs_visitor用adistance_recorder记录树边之间的距离。

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include …
Run Code Online (Sandbox Code Playgroud)

boost boost-graph

1
推荐指数
1
解决办法
654
查看次数

boost asio stable_timer 上的多个递归 async_wait

这个问题的灵感来自于 boost asio 文档(链接)中的异步计时器教程。代码稍作修改,使效果更加明显。

有一个相关的问题,Multiple async_wait from a boost Asio Deadline_timer。但我不确定该问题的答案是否适用于我的情况。

代码非常简单,如果注释掉重复的行,则可以按预期工作,如下所示。

  1. A具有通话steady_timer时长。1sasync_wait

  2. 当它过期时,处理程序被调用。在处理程序内部,计时器的生命周期又延长一秒,并且计时器async_wait再次调用。

  3. 变量count20 用于限制计时器可以触发的次数。


#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>

namespace asio = boost::asio;

void bind_handler(const boost::system::error_code& ec,
                  asio::steady_timer& t,
                  int count) {
  if (count > 0) {
    std::cout << "getting " << count << "\n";
    t.expires_at(t.expiry() + std::chrono::seconds(1));
    t.async_wait(boost::bind(bind_handler, asio::placeholders::error,
                             boost::ref(t), --count));
  }
}

int main() {
  asio::io_context io_context(1);
  asio::steady_timer t(io_context, std::chrono::seconds(1));

  int count = 20; …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-asio

1
推荐指数
1
解决办法
2863
查看次数