In GNU Radio the constructor of all the new blocks are private. It is
done to ensure that a regular C++ pointer can never point at the
constructed object instead a boost shared pointer does the work.
But if the constructor is private how we access them !! This is made possible by declaring a friend function which acts as a "surrogate" constructor.
Details you can find at
http://sumitgnuradio.blogspot.in/2012/10/gnuradio-documentation-part-8-swig.html
So lets see what are friend functions :
In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends.
Friends are functions or classes declared with the
If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend:
Here is a very good example :
// friend functions
#include
using namespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend CRectangle duplicate (CRectangle);
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}
The output is 24.
But if the constructor is private how we access them !! This is made possible by declaring a friend function which acts as a "surrogate" constructor.
Details you can find at
http://sumitgnuradio.blogspot.in/2012/10/gnuradio-documentation-part-8-swig.html
So lets see what are friend functions :
In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends.
Friends are functions or classes declared with the
friend
keyword.If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend:
Here is a very good example :
// friend functions
#include
using namespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend CRectangle duplicate (CRectangle);
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}
The output is 24.
No comments:
Post a Comment