×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Oct 2010
    Location
    chennai
    Posts
    97

    Exclamation constant methods vs non-constant methods

    HI...frdz..
    I need to call a non- constant method(which returns some value) from a constant method.. but when i compile it, it is showing an error.." method (non-static) - function call (const method) does not match"..

    eg..

    int temp_var=0;
    void MyClass::test()const
    {
    temp_var= add(10,20);
    }

    int MyClass::add(int a, int b)
    { return a+b;}
    Thanks & Best Regards
    Srinivas

  2. #2
    Regular Contributor
    Join Date
    Oct 2010
    Location
    chennai
    Posts
    97

    Re: constant methods vs non-constant methods

    I got solution..
    I need to make a local variable (temp_var=0) inside the const method
    Thanks & Best Regards
    Srinivas

  3. #3
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: constant methods vs non-constant methods

    "const" for a method (different from a "const" parameter) means that the method has no effect/dependency on its environment and will (presumably) return the same value every time it's called with a given set of parameters. Therefore, the reference to the global variable "temp_var" is not allowed. "add" is validly const, "test" isn't.

    In addition, the compiler might choose to still flag "test" as non-const, after moving the temp_var definition inside, since "add" is not declared const, and is referenced from "test". But the compiler probably figures out that "add" is safely const without being told.

  4. #4
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: constant methods vs non-constant methods

    You don't need to pre-assign zero to temp_var, just make it local. Eg, "int temp_var;", or combine with the assignment as "int temp_var = add(10,20);".

    Of course, once you make the variable local it can no longer be accessed outside the method.

Similar Threads

  1. variable to constant
    By adhossain in forum Symbian
    Replies: 5
    Last Post: 2010-07-29, 16:47
  2. QA testing methods - questions about sideloading/installation methods, environments,
    By rilano in forum [Closed] Publishing to Nokia Store
    Replies: 9
    Last Post: 2009-06-09, 09:47
  3. Looking for a constant
    By Gixxer2 in forum Symbian
    Replies: 5
    Last Post: 2008-09-29, 09:55
  4. Constant battery charging
    By nuker85 in forum General Development Questions
    Replies: 2
    Last Post: 2007-09-13, 14:24
  5. string constant
    By mprix in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2004-02-09, 17:02

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×