请参阅下面的代码.该drive() 是在范围内,我可以drive的porsche.但是,除非我取消注释声明,否则drive()g ++会在尝试创建仿函数时在此范围错误中声明一个非常奇怪的"驱动器".为什么?
#include <functional>
class car {
friend void drive(const car c);
};
//void drive(const car c);
int main() {
car porsche;
drive(porsche);
std::pointer_to_unary_function<car, void> functor(drive);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更新1:我对ADL的答案几乎感到满意,但是我确实告诉了驱动器参数的类型,它是第一个模板参数,它是汽车:
std::pointer_to_unary_function<car, void> functor(drive);
Run Code Online (Sandbox Code Playgroud)
更新2:好的,这是一个更简单的代码,我们不需要函子和函数头:
class car {
friend void drive(const car c);
};
//void drive(const car c) { }
int main() {
car porsche;
drive(porsche);
void (*f)(const car);
f = drive;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,我理解为什么编译器无法找到driveADL.原因与上面相同,但此代码不会被模板遮盖.
我是Facebook应用程序开发的新手,也是JavaScript和PHP编程的新手.
我目前正在开发一个Facebook应用程序,但目前仍处于Request Dialog窗口.
当请求对话框出现时,我选择我想要的朋友,然后单击"发送请求",执行requestCallback(响应),并按预期通知获取请求的朋友.但是,如果单击"取消"或蓝色关闭按钮,则还会执行requestCallback(响应),但未向选定的朋友通知该请求.
这是我的代码:
function requestCallback(response)
{
//console.log(response);
location.href='step2.php';
}
Run Code Online (Sandbox Code Playgroud)
因此,无论是单击"取消"还是关闭按钮,上面的脚本仍然会执行(转到我指定的页面step2.php.)
我想要的是当用户点击取消按钮或关闭模态窗口按钮时,页面停留在同一页面.
有谁知道如何解决这个问题?
谢谢!
假设我想要一个单独的Child类,并且只有这个类继承自Father并授予Child访问Fathers数据成员的权限.除此之外,没有人应该从父亲继承到其内部,因此我将父亲的数据保密
编辑:我选择了保护,因为我想隐藏数据.有些人认为这是一种良好的做法,但也许它有点过于教条:(例如,Herb Sutter在http://drdobbs.com/184403867)实际上这个课程应该只延长一次.因此,史蒂夫杰西普指出禁止继承将是一种选择,但我认为其成本(虚拟等)对我所需要的东西来说太过分了.
代码看起来像这样:
class Father{
friend class Child;
public:
//functions
private:
int mData;
}
class Child: public Father{
public:
void changeData(int val){mData=val;}
}
Run Code Online (Sandbox Code Playgroud)
我在这里遇到了一些严重的麻烦,或者这是一个有效的决定,假设继承是一个好的决定.
请参阅C++ FAQ lite
它声明了二进制中缀算术运算符:
member functions don't allow promotion of the left hand argument, since that
would change the class of the object that is the recipient of the member
function invocation
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会这样吗?为什么对第一个参数的类型有约束?
谢谢.
以下是一个简单的c ++程序,它使用我的MinGW编译器进行编译,并按预期执行:
#include <iostream>
template <class T> class A {
T a;
template <class U> friend class B;
public:
A<T> (T t) : a(t) {}
};
template <class T> class B {
A<T> aa;
public:
B<T> (T t) : aa(t) {}
T getT() const {return aa.a;}
};
int main() {
B<int> b(5);
std::cout << "> " << b.getT() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
由于B<T>::getT()访问私有 A<T>::a成员,A<T>使得B<T>与朋友template <class U> friend class B;一行.
不幸的是,我不知道为什么 …
头文件包含它:
class Shape_definition {
private:
// ...
std::vector<Instruction> items;
public:
//...
friend std::istream& operator >> (std::istream& is, Shape_definition& def); // FRIEND!
};
//-----------------------------------------------------------------------------
std::istream& operator >> (std::istream& is, Shape_definition& def);
//...
Run Code Online (Sandbox Code Playgroud)
定义代码:
std::istream& operator >> (std::istream& is, Bushman::shp::Shape_definition& def){
//...
Bushman::shp::Instruction instr = Bushman::shp::Instruction::get_empty();
while(is >> instr) def.items.push_back(instr); // Problem is here!
return is;
}
Run Code Online (Sandbox Code Playgroud)
但我在MS Visual Studio编辑器中出错:
错误C2248:'Bushman :: shp :: Shape_definition :: items':无法访问类'Bushman :: shp :: Shape_definition'中声明的私有成员
为什么我不能private在friend运算符中使用该字段?
谢谢.
我有一个A类和一个简单的(非常有限的)迭代器A_iterator指向这个类并成为它的朋友:
#include<vector>
#include<iostream>
using namespace std;
class A_iterator{
public:
A_iterator(int index) : _index(index) {}
int operator*();
private:
int _index;
};
class A{
friend class A_iterator;
public:
typedef A_iterator iterator;
A() { _elems.push_back(1); _elems.push_back(2); }
private:
static vector<int> _elems; //line 19
};
vector<int> A::_elems; // line 22
int A_iterator::operator*()
{
return A::_elems[_index]; //line 26
}
int main()
{
A a;
A::iterator it(0);
cout << *it << endl; //line 33
}
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,我不希望_elems是静态的.如果我将从第19行删除静态关键字并删除第22行,那么我应该如何修改第26行?第33行无法更改.
有很多关于C++好友关键字的帖子.我看到有朋友课的好处.关于封面是否被friend关键字破坏,提出并回答了许多问题,但所有这些响应似乎都是针对使用朋友进行课程的.
但朋友的功能怎么样?我不能为我的生活看到你为什么会这样想.一旦你有任何朋友的功能,你是不是允许任何一个类来访问你所有的私人数据/功能?
class Foo
{
private:
int neverSetToZero;
public:
Foo() { neverSetToZero = 1; }
friend void Whateveryoudowiththisdonttouchneversettozero(Foo * t);
};
void Whateveryoudowiththisdonttouchneversettozero(Foo * t)
{
(*t).neverSetToZero=0;
}
int main()
{
Foo t;
Whateveryoudowiththisdonttouchneversettozero(&t);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 无论如何使用friend使用新定义的结构重载已经声明的struct的构造函数.例如,在下面的简单示例中,我想struct A使用一个使用struct B,但不修改Ah来重载构造函数.
main.cpp中
#include "A.h"
#include "B.h"
int main() {
B x(2);
A y(B);
};
Run Code Online (Sandbox Code Playgroud)
啊
struct A {
float a;
A(float);
};
A::A(float f) { a=f; };
Run Code Online (Sandbox Code Playgroud)
BH
struct B {
float b;
B(float);
B(A);
};
B::B(float f) { b=f; };
B::B(A s) { b=s.a; };
A::A(B s){ a=s.b; };
// ^^^----obviously error since A(B) is not declared,
// but how can I make this work without changing A.h?
Run Code Online (Sandbox Code Playgroud)
我知道我可以用适当的安置解决的问题 …
#include <iostream>
#include <string>
class A {
friend class B;
std::string m = "Hello";
};
struct B {
struct N {
void f(){
A a;
std::cout << a.m << std::endl;
};
};
};
int main() {
B::N().f();
}
Run Code Online (Sandbox Code Playgroud)
是否允许N访问m?
编辑
好吧,我在cpp.sh中运行了它,显然可以运行。是否有通用的规则来了解如何建立友谊关系?
friend ×10
c++ ×9
constructor ×1
facebook ×1
functor ×1
grammar ×1
inheritance ×1
invite ×1
iterator ×1
overloading ×1
request ×1
scope ×1
syntax ×1
visibility ×1