小编Ica*_*rus的帖子

std :: shared_ptr这个

我目前正在尝试学习如何使用智能指针.然而,在做一些实验时,我发现了以下情况,我无法找到一个令人满意的解决方案:

想象一下,你有一个A类的对象是B类对象(孩子)的父对象,但两者都应该互相认识:

class A;
class B;

class A
{
public:
    void addChild(std::shared_ptr<B> child)
    {
        children->push_back(child);

        // How to do pass the pointer correctly?
        // child->setParent(this);  // wrong
        //                  ^^^^
    }

private:        
    std::list<std::shared_ptr<B>> children;
};

class B
{
public:
    setParent(std::shared_ptr<A> parent)
    {
        this->parent = parent;
    };

private:
    std::shared_ptr<A> parent;
};
Run Code Online (Sandbox Code Playgroud)

问题是A类的一个对象如何std::shared_ptr将自身(this)传递给它的孩子?

有Boost共享指针(Get a boost::shared_ptrforthis)的解决方案,但是如何使用std::智能指针处理这个问题?

c++ this shared-ptr this-pointer

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

在Angular4中路由到基本HTML页面

我正在使用Angular 4构建一个网站.除了有一些"复杂"组件之外,我还需要显示一些基本的HTML页面:

  • 开始(登陆页面)
  • 使用条款
  • 版本说明
  • ...

我希望路由器以某种方式进行路由,以便将这些页面的HTML内容放置在 - 的位置<router-outlet></router-outlet>- 就像通常的组件一样.

备注:目前,这些页面不使用任何动态功能,例如绑定; 它们只是基本的HTML.后来我可能想要使用插值来在一个地方定义小文本片段(电话号码).此外,在某些时候,网站需要提供多种语言.但是,目前,只需关注显示基本HTML就足够了.

到目前为止,我发现了两种方法:

方法1:每页一个组件

每个页面都有一个组件.因此,我最终terms-of-use.component.ts,terms-of-use.component.html,imprint.component.ts,imprint.component.html等等).

路由器定义

路由器定义如下:

const routes: Routes = [
    { path: 'start', component: StartComponent },
    { path: 'terms-of-use', component: TermsOfUseComponent },
    { path: 'imprint', component: ImprintComponent },
    ...
]
Run Code Online (Sandbox Code Playgroud)

下行

每个页面都有一个组件意味着很多组件声明.根据页数的不同,这很快就会变得一团糟.

方法1:所有基本页面的一个组件

拥有一个"页面"组件,通过http-request 加载相应站点的HTML内容.我data在路由对象中使用-property来告诉组件,要加载哪个页面.组件的模板用于<div [innerHtml]="pageTemplate"> </div>显示加载的页面内容; 其中pageTemplate持有加载HTML.

路由器定义

路由器定义如下:

const routes: Routes = [
    { path: 'start', component: PageComponent, …
Run Code Online (Sandbox Code Playgroud)

angularjs angular

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

标签 统计

angular ×1

angularjs ×1

c++ ×1

shared-ptr ×1

this ×1

this-pointer ×1