班上的朋友...... [英] friends of a class...
问题描述
大家好。我正试图在我的一个
类中声明一个函数作为朋友。纠正我,如果我错了,但如果我宣布一个友好的函数
,这意味着我可以在该函数中访问我的班级成员?这个
一直用Borland C ++为我工作但是一旦我改为g ++我就会得到错误。这是一个简短的例子,我希望能够从我的朋友功能测试()中访问我的
班级成员:
#include< iostream> ;
使用命名空间std;
class myfirstclass
{
private:
public:
int number;
void greeting();
friend void test();
};
无效测试()
{
cout<< myfirstclass-> number;
}
void myfirstclass :: greeting()
{
for(int i = 0; i< = number; i ++)
{
cout<< number;
}
}
int main()
{
myfirstclass myfirstobject;
myfirstobject.number = 3;
myfirstobject.greeting();
返回0;
}
非常感谢任何帮助,
Rory。
你的朋友 - 功能不能知道你的班级;-)
朋友功能可以访问班级的私人会员,但它是
必须已经知道你的班级 - 对象。
你的函数必须这样定义:
friend void test(const myfirstclass& ob )
{
cout<< ob.number;
}
>
干杯。但那么将其宣布为朋友有什么意义呢?我可以
就这样做
class myfirstclass
{
private:
public:
int number;
void greeting();
};
void test(myfirstclass * test);
这不是一回事吗?实际上也许我在这里咆哮错误的
树。让我告诉你更多关于为什么我认为朋友
关键字是我需要的东西。我需要调用启动线程的
应用程序中的函数。启动线程函数必须是
传递一个线程函数,声明为
uintptr_t csThread(void * clientData)
事情是我需要这个线程函数才能访问我班级的
成员但是我不能将我的类作为参数传递给出
启动线程函数有如上所述。我有这个
应用程序使用Borland的C ++进行工作和构建它不知何故它
工作正常,我可以访问我班级的所有成员,但我是现在移植
应用程序并使用g ++它会产生错误。明显。任何想法
就我能做什么?我宁愿使用我使用的API提供的线程函数
而不是切换。干杯,
Rory。
哪些错误?
请在这里写下来;-)
朋友的功能不是好朋友。你只需要它,即当你需要超载操作员的时候。
我(全部)其他你不需要它们的情况。
请告诉我你的错误并发布错误发生的代码。
//如果我的英语不是来自德国的最好的话,我会很沮丧; - )
Hi everyone. I''m trying to declare a function as a friend in one of my
classes. Correct me if I''m wrong but if I declare a friendly function
that means that I can access member of my class in that function? This
had been working for me with Borland C++ but once I changed to g++ I
get errors. Here''s a brief example, I want to be able to access my
classes members from my friend function test():
#include <iostream>
using namespace std;
class myfirstclass
{
private:
public:
int number;
void greeting();
friend void test();
};
void test()
{
cout << myfirstclass->number;
}
void myfirstclass::greeting()
{
for(int i = 0; i<= number;i++)
{
cout<<number;
}
}
int main ()
{
myfirstclass myfirstobject;
myfirstobject.number =3;
myfirstobject.greeting();
return 0;
}
Any help is much appreciated,
Rory.
Hi,
your friend-function can''t know your class ;-)
A friend-function can acces to the private Member of the class but it
must already know your class-OBJECT.
Your function must be defined like this:
friend void test(const myfirstclass& ob)
{
cout<<ob.number;
}
Cheers. But then what''s the point in declaring it as a friend? I could
just do this
class myfirstclass
{
private:
public:
int number;
void greeting();
};
void test(myfirstclass* test);
Isn''t that the same thing? Actually perhaps I''m barking up the wrong
tree here. Let me know tell you more about why I thought the friend
keyword was the thing I needed. I need to call a function in my
application that starts a thread. The start thread function must be
passed a thread function that''s declared as
uintptr_t csThread(void *clientData)
The thing is that I need this thread function to be able to access
members of my class yet I can''t pass my class as a parameter given that
the start thread function has to be declared as above. I have this
application working and building using Borland''s C++ and somehow it
works fine, I can access all the member of my class, but I''m porting
the application now and using g++ it gives errors. Obviously. Any ideas
on what I can do? I''d rather use the thread functions that are provided
by the API I am using rather than switching. Cheers,
Rory.
Which errors ??
Write them here please ;-)
A friend function isn''t a good friend. You only need it i.e. when you
need to overload operators.
I (all) other cases you don''t need them.
Please tell me your errors and post the code where the error occurs.
//Sry if my english isn''t the best i''m from germany ;-)
这篇关于班上的朋友......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!