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 6572921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:04:01+00:00 2026-05-25T15:04:01+00:00

I need to implement a mock for an interface that is defined something like:

  • 0

I need to implement a mock for an interface that is defined something like:

class Foo
{
public:
  void sendEvent(int id) const = 0;
}

My mock class needs to save all event id’s sent to the class. This is how I intended to do it.

class FooMock : Foo
{
private: 
  m_vector std::vector<int>;
public:
  void sendEvent(int id) const {m_vector.push_back(id);}

}

But obviously the compiler refuses that construction. Are there any solutions to this (assuming the interface could not be changed)?

I realize that I can use two classes for this. But isn’t there a way to shut the compiler up and allow me to to this, similar to const_cast?

  • 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-25T15:04:02+00:00Added an answer on May 25, 2026 at 3:04 pm

    You can make the vector mutable so that it can be modified from within const methods, like this:

    mutable std::vector<int> m_vector;
    

    Note however that this makes the vector mutable from all methods. If you only want to write to it from a single method, a const_cast is less invasive, in that you cast the constness of this away just for a single call:

    FooMock * const that = const_cast<FooMock * const>(this);
    that->m_vector.push_back(id);
    

    I’m being a bit pedantic here – inside a const method, this has the type T const * const (so both the object being pointed to as well as the pointer itself are const). The const_cast just casts away the constness of the object, but not of the pointer.

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

Sidebar

Related Questions

I have a class like this: public final class Foo { public native int
When the class with a member which is a pointer, we need implement a
I need to implement a browser plugin which can register its own protocol (like
I'm new to mock objects, but I understand that I need to have my
I understand the need to test a class that has logic (for instance, one
I need implement a Data entry like receiver field in mail app (Ex. To:
I need to implement a chat system for a website, so that customers of
I need to implement a server that can send Push Notifications to Android mobile
I need to implement the overloaded the assignment operator in a class so the
I need to implement a Diff algorithm in VB.NET to find the changes between

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.