在初始化列表中使用“this”指针是否安全?(Is it safe to use the “this” pointer in an initialization list?)
我有两个父子关系的类(
Parent
类“has-a”Child
类),而Child
类有一个指针返回到Parent
。 在构建孩子时初始化父指针是很好的,如下所示:class Child { public: Child (Parent* parnet_ptr_) : parent_ptr(parent_ptr_) {}; private: Parent* parent_ptr; }; class Parent { public: Parent() : child(this) {}; private: Child child; }
现在,我知道人们建议不要在初始化列表中使用
this
,而C ++常见问题解答说我会得到一个编译器警告(BTW,在VS2010,我没有得到警告),但我真的很喜欢这个更好,然后调用一些设置在Parent
构造函数中的函数。 我的问题是:
- 当父对象被创建时,父指针是否定义良好?
- 如果是这样,为什么要像上面那样使用它是坏的做法?
谢谢,
波阿斯
编辑:感谢Timbo,确实是重复的 (嗯,我甚至选择了相同的类名)。 所以我们可以获得一些附加值:引用如何? 执行以下操作是否可能/安全? :
class Child { public: Child (Parent& parnet_ptr_) : parent_ptr(parent_ptr_) {}; private: Parent* parent_ptr; }; class Parent { public: Parent() : child(*this) {}; private: Child child; }
I have two classes with a parent-child relationship (the
Parent
class "has-a"Child
class), and theChild
class has a pointer back to theParent
. It would be nice to initialize the parent pointer upon construction of the child, as follows:class Child; class Parent; class Child { public: Child (Parent* parent_ptr_) : parent_ptr(parent_ptr_) {}; private: Parent* parent_ptr; }; class Parent { public: Parent() : child(this) {}; private: Child child; }
Now, I know people recommend not using
this
in initialization list, and C++ FAQ says I'm gonna get a compiler warning (BTW, on VS2010, I don't get a warning), but I really like this better then calling some set function inParent
's constructor. My questions are:
- Is the parent
this
pointer well-defined when theChild
object is being created?- If so, why is it considered bad practice to use it as above?
Thanks,
Boaz
EDIT: Thanks Timbo, it is indeed a duplicate (huh, I even chose the same class names). So lets get some added value: how about references? Is it possible / safe to do the following? :
class Child { public: Child (Parent& parnet_ptr_) : parent_ptr(parent_ptr_) {}; private: Parent* parent_ptr; }; class Parent { public: Parent() : child(*this) {}; private: Child child; }
原文:https://stackoverflow.com/questions/5058349
最满意答案
问题是因为您正在制作跨域AJAX请求,这会阻止浏览器安全性 - 请参阅同源策略 。
该请求期望您向启用CORS的域发出请求,因此它会抱怨不存在的标头。
您需要将请求更改为
jsonp
类型,或使用服务器端代理来获取数据。The problem is because you are making a cross-domain AJAX request, which prevented by browser security - see the Same Origin Policy.
The request is expecting you to be making a request to a CORS enabled domain, hence why it is complaining about the non-existant header.
You either need to change your request to
jsonp
type, or use a server-side proxy to get the data.
相关问答
更多-
错误消息说: 没有'Access-Control-Allow-Origin'标题 您已设置三个Access-Control-Allow-SOMETHING标题,但它们都不是Origin 。 The error message says: No 'Access-Control-Allow-Origin' header You have set three Access-Control-Allow-SOMETHING headers, but none of them is Origin.
-
浏览器将使用OPTIONS方法执行预检请求以检查对资源的访问。 您需要在JAX-RS后端中实现一个方法,以响应带有CORS头的OPTIONS请求(正如您在“add”方法中所做的那样)。 @OPTIONS public Response options() { return Response .status(Response.Status.OK) .header("Access-Control-Allow-Origin", "*") ...
-
问题是因为您正在制作跨域AJAX请求,这会阻止浏览器安全性 - 请参阅同源策略 。 该请求期望您向启用CORS的域发出请求,因此它会抱怨不存在的标头。 您需要将请求更改为jsonp类型,或使用服务器端代理来获取数据。 The problem is because you are making a cross-domain AJAX request, which prevented by browser security - see the Same Origin Policy. The request is ...
-
基本上,为了制作一个跨域AJAX请求,请求的服务器应该允许资源的交叉原始共享(CORS)。 您可以从这里阅读更多: http : //www.html5rocks.com/en/tutorials/cors/ 在您的方案中,您正在将客户端中的标题设置为实际需要设置为http:// localhost:8080 / app服务器端代码。 如果您使用PHP Apache服务器,那么您需要在.htaccess文件中添加以下内容: Header set Access-Control-Allow-Origin "*" ...
-
该响应具有HTTP状态码405 看看那个 : 405方法不允许 响应预检请求 看看那个 : “preflighted”请求首先通过OPTIONS方法向另一个域上的资源发送HTTP请求,以确定实际请求是否安全发送 假设您的服务器端代码设置为将CORS头添加到对POST请求的响应中,但是您尚未设置它来处理OPTIONS请求。 The response had HTTP status code 405 Look that up: 405 Method Not Allowed Response to preflig ...
-
我会将它添加到你的application.rb中 config.action_dispatch.default_headers = { 'Access-Control-Allow-Origin' => '*', 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") # or whatever else you would like to allow } 适合我。 I can't solve the problem ...
-
React Redux
) [2023-06-11]如果您的React dev服务器源不同于Rails应用程序服务器源(即使只是端口号),Rails认为该请求是跨源的。 当Rails收到此类请求时,除非设置了CORS策略,否则它会拒绝它。 最简单的方法是(可能)使用rack-cors gem: 1)添加 gem 'rack-cors', require: 'rack/cors' 到您的Gemfile 。 2) bundle install 3)添加 # see gem page for more examples config.middleware.ins ... -
实际上,它是服务器端问题,而不是Angular2问题。 预检的OPTIONS请求需要在其响应中返回Access-Control-Allow-Origin标头。 有关详细信息,请参阅这些文章 http://restlet.com/blog/2015/12/15/understanding-and-using-cors/ http://restlet.com/blog/2016/09/27/how-to-fix-cors-problems/ In fact, it's server-side issue and ...
-
在服务器上路由之前,请添加: app.all('/*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); }); 您可以将*替换为授权域的URL。 更多信息: https : //developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Before your routing on your server, add : app.al ...
-
出于安全原因,浏览器限制从脚本内发起的跨源HTTP请求。 Postman是一个REST客户端,在Chrome浏览器中作为应用程序运行。 它专门用于测试其余API,因此不限制跨源资源请求。 What happen is for security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts.Postman is a REST Client that runs as an applicati ...