CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Force a friend outside of class declaration?

  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    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.

  2. #2
    Join Date
    Feb 2017
    Posts
    571

    Re: Force a friend outside of class declaration?

    Quote Originally Posted by babaliaris View Post
    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.

  3. #3
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Re: Force a friend outside of class declaration?

    Quote Originally Posted by wolle View Post
    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!

  4. #4
    Join Date
    Feb 2017
    Posts
    571

    Re: Force a friend outside of class declaration?

    Quote Originally Posted by babaliaris View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)