|
-
November 6th, 2008, 11:27 PM
#1
Friend class
I would like to declare Estimator class a friend of YLC2Plot class. Any ideas??
Thanks in advance.
Regards: Shabbar Abbas
i am using sun c++ 5.9 ( CC )
*********************************************/
#ifndef _ESTIMATOR_H
#define _ESTIMATOR_H
template<typename RadarType, typename FilterType>
class IMM: public RadarType, public FilterType {
public:
//....
protected:
//...
private:
//..
};
#endif
/******************************************/
#ifndef _YLC2PLOT_H
#define _YLC2PLOT_H
#include "Estimator.h"
class YLC2Plot : public BasePlot {
public:
friend IMM;
protected:
//....
private:
//.....
};
#endif /* _YLC2PLOT_H */
Last edited by sza110; November 6th, 2008 at 11:53 PM.
Reason: sorry, actually i want IMM class to be friend of YLC2Plot class
-
November 6th, 2008, 11:42 PM
#2
Re: Friend class
You are doing it backwards. You have declared IMM a friend of YLC2Plot.
The Freient declaration goes into the class that you want another class to have access to, it does NOT go into the "friend" class.
So modifying the IMM class declaration to include Estimator as a friend is the solution.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 7th, 2008, 04:33 AM
#3
Re: Friend class
Can unspecialised template classes even be friends? Which class are you specifying?
IMM is not a class - it's a template from which the compiler can generate classes. The only way you can make it a friend is to fully specialise all the template parameters of IMM in the friend declaration, so that you have an actual class to make friends with, not a template.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 7th, 2008, 05:30 AM
#4
Re: Friend class
Annoyingly friendship isn't inherited. However one workaround might be to create a YLC2Plot_Friend class declare it a friend of YLC2Plot and put a bunch of get/set methods and fuction passthoughs that access everything private. Then make the class IMM inherit from YLC2Plot_Friend.
Its not pretty, but it will give IMM and all things that inherit from it the required functionality.
-
November 7th, 2008, 09:06 AM
#5
Re: Friend class
 Originally Posted by Graham
Can unspecialised template classes even be friends? Which class are you specifying?
IMM is not a class - it's a template from which the compiler can generate classes. The only way you can make it a friend is to fully specialise all the template parameters of IMM in the friend declaration, so that you have an actual class to make friends with, not a template.
OUCH...I missed the template... 
One alternative (which I tend to use ALOT) is to derive my template classes from a non-templated base.
a) It clearly deliniates specialized from general aspects
b) It *may* reduce code size
c) You can friend other classes to the base easily...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 03:28 AM
#6
Re: Friend class
thankyou all for the ideas. I finally nailed it. Thnaks again
-
November 11th, 2008, 04:24 AM
#7
Re: Friend class
 Originally Posted by TheCPUWizard
It clearly deliniates specialized from general aspects ...
... You can friend other classes to the base easily
here you seem to restrict friendship to the general (base) type;
Is this intentional ? Then, do you use an accessor/mutator pattern as suggested by couling ?
In some sense I tend to do the opposite (as in sza110 code), i.e. I friend a "secondary" class when it needs to access inherently specific parts of a "primary" class. Is this good practice ?
-
November 11th, 2008, 08:03 AM
#8
Re: Friend class
 Originally Posted by superbonzo
here you seem to restrict friendship to the general (base) type;
Is this intentional ? Then, do you use an accessor/mutator pattern as suggested by couling ?
In some sense I tend to do the opposite (as in sza110 code), i.e. I friend a "secondary" class when it needs to access inherently specific parts of a "primary" class. Is this good practice ?
As posted previously there are some issues with friending specializations. If the relationship is sich that it does not require access to the specialization itself (which is often the case) then my approach may make things cleaner.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 12:51 PM
#9
Re: Friend class
 Originally Posted by Graham
Can unspecialised template classes even be friends?
I thought they could. And in that case, all instantiations would be a friend to the class the template is declared as a friend of. The following test code compiled with Comeau online as well with VC++ 2005:
Code:
class SomeType
{
int data;
public :
SomeType() : data(0){}
template<class T>
friend struct TemplateClass;
};
template<class T>
struct TemplateClass
{
void accessAndChangePrivate(SomeType& ref)
{
ref.data = 10;
}
};
int main()
{
TemplateClass<int> obj;
TemplateClass<float> obj2;
SomeType objST;
obj.accessAndChangePrivate(objST);
obj2.accessAndChangePrivate(objST);
}
The OP's syntax looks inappropriate though.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
November 11th, 2008, 01:01 PM
#10
Re: Friend class
 Originally Posted by exterminator
I thought they could. And in that case, all instantiations would be a friend to the class the template is declared as a friend of. The following test code compiled with Comeau online as well with VC++ 2005[</snip>
Here you are making all of the specializations of TemplateClass friends of the non-template class SomeType.
I understand the issue to be more of the other way aound...
That is to make class X a friend of a specialized templated class. In otherwords make class X a friend of TemplateClass<int>.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 01:05 PM
#11
Re: Friend class
 Originally Posted by TheCPUWizard
Here you are making all of the specializations of TemplateClass friends of the non-template class SomeType.
I understand the issue to be more of the other way aound...
I don't think so, but then I am feeling sleepy now. IMM is the class template declared as friend. So, we are talking of 'friend class templates' which is exactly what I tried to put-in in the sample code.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
November 11th, 2008, 01:09 PM
#12
Re: Friend class
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|