-
September 27th, 2020, 09:22 AM
#1
Force a friend outside of class declaration?
Is it possible to force a class to have a friend class or friend function outside of this class declaration?
For example
Code:
class MyClass
{
. . .
private:
int m_id = 0;
};
//Somewhere else
force MyClass to have friend int AccessPrivateSruff(const MyClass& obj);
int AccessPrivateSruff(const MyClass& obj)
{
return obj.m_id;
}
The reason I want to do that, is because I'm writting a unit test framework and
I want somehow to add my unit test class as a friend to the classes that I'm testing
but I dont want to change the declarations of the actual classes that I'm testing.
It does not make sense my actual application classes to have friends of the testing framework,
since the testing framework is not part of the application.
Last edited by babaliaris; September 27th, 2020 at 09:25 AM.
-
September 27th, 2020, 11:54 AM
#2
Re: Force a friend outside of class declaration?
Originally Posted by babaliaris
It does not make sense my actual application classes to have friends of the testing framework,
since the testing framework is not part of the application.
You could use the preprocessor to define a macro that looks like this while testing,
#define UNITTEST_FRIEND int AccessPrivateSruff(const MyClass& obj);
and otherwise like this,
#define UNITTEST_FRIEND
The bad news is that you need too add UNITTEST_FRIEND to MyClass so it will be modified, but the good news is that the friend function will only be present during testing.
Last edited by wolle; September 27th, 2020 at 12:00 PM.
-
September 28th, 2020, 12:55 AM
#3
Re: Force a friend outside of class declaration?
Originally Posted by wolle
You could use the preprocessor to define a macro that looks like this while testing,
#define UNITTEST_FRIEND int AccessPrivateSruff(const MyClass& obj);
and otherwise like this,
#define UNITTEST_FRIEND
The bad news is that you need too add UNITTEST_FRIEND to MyClass so it will be modified, but the good news is that the friend function will only be present during testing.
Yeap this seems to be the only solution. I checked googletest to see how they do it, and their implementation is excactly like your answer!
Thank you!
-
September 28th, 2020, 11:33 AM
#4
Re: Force a friend outside of class declaration?
Originally Posted by babaliaris
Yeap this seems to be the only solution.
Well, I'm sure there is some ugly trick to do it but using the preprocessor is better I think because then you don't break the C++ language itself. The preprocessor allows you to select which parts of a C++ source code should participate in compilation and that's much less intrusive. But it can turn the code base into a mess so one has to use it with care.
Last edited by wolle; September 28th, 2020 at 11:36 AM.
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
|