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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:16:11+00:00 2026-06-14T03:16:11+00:00

I was experimenting with the const keyword and trying to get an useful approach

  • 0

I was experimenting with the const keyword and trying to get an useful approach from it.

#include <iostream>

class A
{
  public:
  static const void modify(float& dummy)
  {
    dummy = 1.5f;
  }  
};

int main(int argc, char* argv[])
{
  auto a = 49.5f;

  A::modify(a);

  std::cout << a << std::endl; 

  return(0);
}

this code compiles and works, the output is 1.5, I was expecting an error from the compiler because I have a const method that is trying to modify the value of an argument.

What I’m missing here ? How i can design methods that will not modify argument’s values?

  • 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-06-14T03:16:13+00:00Added an answer on June 14, 2026 at 3:16 am
    1. The method you declared is not const. It returns a const void (whatever that is), but it is not a const-method itself.

    2. If it were declared

      void modify(float& dummy) const
      

      it would be a const-method, but then still it could modify the value of the argument, because a const-method is allowed to do this. The only thing it is not allowed to do is to modify values of members of the class it belongs to.

    3. Note that in order to declare a const method, I had to remove the static specification. A static method can never be const, because a static method can’t modify any members anyway.

    4. If you want to prevent the function from modifying its argument, you’d have to make the argument const:

      static void modify(const float& dummy)
      

    To illustrate what a const-method can and cannot do, here is a class that has a member, and a const-function:

    class A
    {
      float my_member;
    public:
      void modify(float& dummy) const
      {
        my_member = dummy; // attempt to modify member -> illegal
        dummy = 1.5f;      // modifies a non-const argument -> ok
      }  
    };
    

    As you can see, it cannot modify a member, but it can modify the value of its argument. If you want to prevent that, you need to make the argument const:

    class A
    {
      float my_member;
    public:
      void modify(const float& dummy) const
      {
        my_member = dummy; // attempt to modify member -> illegal
        dummy = 1.5f;      // attempt to modify a const reference -> illegal
      }  
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experimenting with ZeroMQ and trying to get something working. My first thought was
I am experimenting with const_cast operator and trying to override a const status of
Experimenting with Scala... I'm trying to define something analogous to the @ hack in
I'm experimenting with Spring Mobile but I can't seem to get the basic example
I'm experiencing a weird problem with stringstream . #include stdafx.h #include iostream #include sstream
I'm trying to convert some code from Python to C++ in an effort to
I'm experimenting with writing a wrapper class for SQLite to be exposed through a
I'm trying a different approach to something that I was working on the other
I'm experimenting with XCode and trying to compile someone else's Windows code. There's this:
I run this code for experimenting copy constructor and assignment operator class AClass {

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.