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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:02:29+00:00 2026-05-25T10:02:29+00:00

I have code like this… class Time { public: Time(int, int, int); void set_hours(int);

  • 0

I have code like this…

class Time
{
    public: 
        Time(int, int, int);
        void set_hours(int);
        void set_minutes(int);
        void set_seconds(int);

        int get_hours() const;
        int get_minutes() const;
        int get_seconds() const;

        static void fun() ;

        void printu() const;
        void prints();

    private:
        int x;
        int hours;
        int minutes;
        int seconds;
        const int i;
};

Why do I need to put const at last to make a function constant type but if i need to make a function, I can do this like…

static void Time::fun() 
{
    cout<<"hello";
}

Above function fun() is also in same class. I just want to know what is the reason behind this?

  • 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-25T10:02:30+00:00Added an answer on May 25, 2026 at 10:02 am

    with a const instance method such as int get_hours() const;, the const means that the definition of int get_hours() const; will not modify this.

    with a static method such as static void fun();, const does not apply because this is not available.

    you can access a static method from the class or instance because of its visibility. more specifically, you cannot call instance methods or access instance variables (e.g. x, hours) from the static method because there is not an instance.

    class t_classname {
    public:
      static void S() { this->x = 1; } // << error. this is not available in static method
      void s() { this->x = 1; } // << ok
      void t() const { this->x = 1; } // << error. cannot change state in const method
      static void U() { t_classname a; a.x = 1; } // << ok to create an instance and use it in a static method
      void v() const { S(); U(); } // << ok. static method is visible to this and does not mutate this.
    
    private:
      int a;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like this: NSData *data = [NSData dataWithContentsOfURL:objURL]; const void *buffer =
i have code like this public class People { public string name { get;
Suppose I have code like this: template<class T, T initial_t> class Bar { //
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)
I have code like this: type TMyDictionary = TDictionary<int, int>; var myDict: TMyDictionary; k,
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
I have code like this: class ToBeTested { function simpleMethod($param) { if(0 === $param)
If I have code like this: class Person { $age; $height; $more_stuff_about_the_person; function about()
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T

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.