循环定义问题。 [英] cyclic defination problem.

查看:71
本文介绍了循环定义问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有一个名为Session的类,它存储一个Page的向量,比如

vector< PageAlso每个页面需要'知道它所在的Session

附加。


因此我有,(这些课有很多其他的东西,我只复制了我需要的

部分)
$ b Session.hpp $ b

#include" Page.hpp"

class Session {

private:

std :: vector< Page_pages;

};


在Page.hpp

class Session; // forward decl

class Page {

private:

Session * _session;

public:

页面(会话*会话):_session(会话){}

};

它是删除循环定义问题的最佳方法吗?或者我的任何方式
可以存储对Session的引用而不是指向Session的指针吗?


谢谢

abir

Hi I have a class called Session, which stores a vector of Page, like
vector<PageAlso each Page need''s to know the Session to which it is
attached.

Thus I have, (the classes has many other things, I copied only the
portion i needed)
in Session.hpp

#include "Page.hpp"
class Session {
private:
std::vector<Page_pages;
};

while in Page.hpp
class Session;// forward decl
class Page{
private:
Session* _session;
public:
Page(Session* session) : _session(session) {}
};
Is it the best way to remove cyclic definition problem? Or any way I
can store a reference to Session rather than pointer to Session?

Thanks
abir

推荐答案

toton写道:
toton wrote:

你好我有一个名为Session的类,它存储Page的向量,例如

vector< PageAlso每个页面需要'知道它所附的会话

附加。


因此我有,(这些课有很多其他的东西,我只复制了我需要的

部分)

在Session.hpp中

#include" Page.hpp"

class Session {

private:

std :: vector< Page_pages; <在Page.hpp中
};




class Session; // forward decl

class页面{

私人:

会话* _session;

公开:

页面(会话*会话):_session (会话){}

};

这是删除的最佳方式循环定义问题?或者我的任何方式
可以存储对Session的引用而不是指向Session的指针?
Hi I have a class called Session, which stores a vector of Page, like
vector<PageAlso each Page need''s to know the Session to which it is
attached.

Thus I have, (the classes has many other things, I copied only the
portion i needed)
in Session.hpp

#include "Page.hpp"
class Session {
private:
std::vector<Page_pages;
};

while in Page.hpp
class Session;// forward decl
class Page{
private:
Session* _session;
public:
Page(Session* session) : _session(session) {}
};
Is it the best way to remove cyclic definition problem? Or any way I
can store a reference to Session rather than pointer to Session?



是否存储对Session的引用或指向Session的指针对你来说是
,要么需要使用某种转发声明,

你已经完成了。请记住,如果您在''Page''中存储参考

,则必须在构建Page

对象时初始化它,否则您将无法在页面的生命周期内更改它。

如果符合您的设计,请选择参考。否则,请使用

指针。


V

-

请删除资金' 'A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问

Whether to store a reference to Session or a pointer to Session is up
to you, and either would require using some kind of forward declaration,
which you have done already. Keep in mind that if you store a reference
in ''Page'' you will have to initialise it during construction of the Page
object and you will not be able to change it during the Page''s lifetime.
If that fits your design, prefer the reference. Otherwise, go with the
pointer.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


toton写道:
toton wrote:

你好我有一个名为Session的类,它存储一个Page的向量,比如

vector< PageAlso each Page needs'要知道它所在的会话

附上。


因此我有,(这些课有很多其他的东西,我只复制了部分)
Session.hpp中的


#include" Page.hpp"

课程会话{

私人:

std :: vector< Page_pages;

};


在Page.hpp

class Session; // forward decl

class Page {

private:

Session * _session;

public:

Page(Session * session):_session(s ession){}

};

这是删除循环定义问题的最佳方法吗?或者我的任何方式
可以存储对Session的引用而不是指向Session的指针?
Hi I have a class called Session, which stores a vector of Page, like
vector<PageAlso each Page need''s to know the Session to which it is
attached.

Thus I have, (the classes has many other things, I copied only the
portion i needed)
in Session.hpp

#include "Page.hpp"
class Session {
private:
std::vector<Page_pages;
};

while in Page.hpp
class Session;// forward decl
class Page{
private:
Session* _session;
public:
Page(Session* session) : _session(session) {}
};
Is it the best way to remove cyclic definition problem? Or any way I
can store a reference to Session rather than pointer to Session?



你可以有一个参考(它很简单,可以测试 - 你试过吗?/ b
吗?)。有关前向声明和破坏

依赖关系的更多信息,请参阅此文章:

http://www.gotw.ca/publications/mill04.htm


干杯! --M

You could have a reference (it''s simple enough to test -- did you try
it?). See this article for more on forward declarations and breaking
dependencies:

http://www.gotw.ca/publications/mill04.htm

Cheers! --M


" mlimber" < ml ***** @ gmail.com写信息

新闻:11 ********************** @ h48g2000cwc。 googlegr oups.com
"mlimber" <ml*****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com

toton写道:
toton wrote:

>嗨我有一个名为Session的类,其中存储一个Page的向量,比如
vector< PageAlso每个页面需要'来知道它所连接的Session。

因此我有,(这些类有很多其他的东西,我只复制了我需要的
部分在Session.hpp

#include" Page.hpp"
课程会议{
private:
std :: vector< Page_pages;
};

在Page.hpp中
类Session; //转发decl
类Page {
私人:
会话* _session;
公开:
页面(会话*会话):_session(session){}
};
是吗删除循环定义问题的最佳方法是什么?或者我可以以任何方式存储对Session的引用而不是指向Session的指针?
>Hi I have a class called Session, which stores a vector of Page, like
vector<PageAlso each Page need''s to know the Session to which it is
attached.

Thus I have, (the classes has many other things, I copied only the
portion i needed)
in Session.hpp

#include "Page.hpp"
class Session {
private:
std::vector<Page_pages;
};

while in Page.hpp
class Session;// forward decl
class Page{
private:
Session* _session;
public:
Page(Session* session) : _session(session) {}
};
Is it the best way to remove cyclic definition problem? Or any way I
can store a reference to Session rather than pointer to Session?



你可以有一个参考(它很简单,可以测试 - 你试过吗?/ b
吗?)。


You could have a reference (it''s simple enough to test -- did you try
it?).



使用引用有点棘手。


页面存储在向量中,存储在标准容器中的对象必须是

可分配。编译器不会为Page,

生成赋值运算符,因为无法重新定位引用。


您可以为Page编写赋值运算符要获得编译的代码,

但是任何用户定义的赋值运算符同样不会重新定位

引用,因此可能会出现一些意外行为。


-

John Carson

Use of a reference is a little tricky.

Page is stored in a vector and objects stored in standard containers must be
assignable. The compiler won''t generate an assignment operator for Page,
because a reference cannot be re-seated.

You could write an assignment operator for Page to get the code to compile,
but any user-defined assignment operator likewise will not re-seat a
reference, so there could be some unexpected behaviour.

--
John Carson


这篇关于循环定义问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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