我目前正在尝试学习如何使用智能指针.然而,在做一些实验时,我发现了以下情况,我无法找到一个令人满意的解决方案:
想象一下,你有一个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::智能指针处理这个问题?
我正在使用Angular 4构建一个网站.除了有一些"复杂"组件之外,我还需要显示一些基本的HTML页面:
我希望路由器以某种方式进行路由,以便将这些页面的HTML内容放置在 - 的位置<router-outlet></router-outlet>- 就像通常的组件一样.
备注:目前,这些页面不使用任何动态功能,例如绑定; 它们只是基本的HTML.后来我可能想要使用插值来在一个地方定义小文本片段(电话号码).此外,在某些时候,网站需要提供多种语言.但是,目前,只需关注显示基本HTML就足够了.
到目前为止,我发现了两种方法:
每个页面都有一个组件.因此,我最终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)
每个页面都有一个组件意味着很多组件声明.根据页数的不同,这很快就会变得一团糟.
拥有一个"页面"组件,通过http-request 加载相应站点的HTML内容.我data在路由对象中使用-property来告诉组件,要加载哪个页面.组件的模板用于<div [innerHtml]="pageTemplate">
</div>显示加载的页面内容; 其中pageTemplate持有加载HTML.
路由器定义如下:
const routes: Routes = [
{ path: 'start', component: PageComponent, …Run Code Online (Sandbox Code Playgroud)