Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 812603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:13:41+00:00 2026-05-15T01:13:41+00:00

This is a revised/better written version of the question I asked earlier today —

  • 0

This is a revised/better written version of the question I asked earlier today — that question is deleted now.

I have a project where I’m getting started with Google Mock. I have created a class, and that class calls functions whithin the Windows API. I’ve also created a wrapper class with virtual functions wrapping the Windows API, as described in the Google Mock CheatSheet. I’m confused however at how I should pass the wrapper into my class that uses that object. Obviously that object needs to be polymorphic, so I can’t pass it by value, forcing me to pass a pointer. That in and of itself is not a problem, but I’m confused as to who should own the pointer to the class wrapping the API.

So… how should I pass the wrapper class into the real class to facilitate mocking?

Here’s an example of what I mean:

struct A {
    virtual void SomeMethod(int x, int y)
    {
        ::SomeMethod(x, y);
    };
};

class Client
{
    A * method_;
public:
    Client(A * method = new A) : method_(method) {};
    void DoSomething()
    {
        method_->SomeMethod(42, 34);
    }
};

struct Mock : public A
{
    MOCK_METHOD2(SomeMethod, void(int, int));
};

TEST(MyTest, MyTestWithMock)
{
    Mock * mock = new Mock();
    EXPECT_CALL(*mock, SomeMethod(42, 34)).Times(1);
    Client client(mock); //Should *client* be responsable for deleting mock?
    client.DoSomething();
};

EXAMPLE 2:

struct A {
    virtual void SomeMethod(int x, int y)
    {
        ::SomeMethod(x, y);
    };
};

class Client
{
    A * method_;
public:
    Client(A * method) : method_(method) {};
    static Client Create()
    {
        static A;
        return Client(&A);
    }
    void DoSomething()
    {
        method_->SomeMethod(42, 34);
    }
};

struct Mock : public A
{
    MOCK_METHOD2(SomeMethod, void(int, int));
};

TEST(MyTest, MyTestWithMock)
{
    Mock  mock;
    EXPECT_CALL(mock, SomeMethod(42, 34)).Times(1);
    Client client(&mock);
    client.DoSomething();
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:13:42+00:00Added an answer on May 15, 2026 at 1:13 am

    There’s always the option of using shared_ptr; that would certainly solve the ownership problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a moot question as I'm not on this project any more, but
Revised: I have a list items on the page that I'm creating from database
This is kinda oddball, but I was poking around with the GNU assembler today
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
This is a self-explanatory question: Why does this thing bubble into my try catch's
Archaelus suggested in this post that writing a new format routine to handle named
UPDATE: I added an answer to this question which incorporates almost all the suggestions
This is beyond both making sense and my control. That being said here is
I have found this example on StackOverflow: var people = new List<Person> { new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.