Results 1 to 4 of 4

Thread: Problem with static and inline in C++

  1. #1
    Join Date
    Dec 2008
    Posts
    150

    Problem with static and inline in C++

    I have class with a counter declared as static and I defined an assessor on the variables

    The following code works:

    Code:
    class {
        static int counter; 
      ... 
        static int getCounter () {return counter;} 
      };
    The method defined in the header of a class are inline by default. You can do this by defining the methods "inline" in the *.h but outside the class.

    But the following code does not work:

    Code:
      class {
        static int counter; 
      ... 
      }; 
      inline static int getCounter () {return counter;}
    I have the impression that the compiler makes an error because it can not distinguish that it is a static method of a method that has a static return type.

    Someone to a solution, please?

  2. #2
    Join Date
    Feb 2008
    Posts
    194

    Re: Problem with static and inline in C++

    The method is not "a static return type."

    Your code does not work, because your function is not part of the class, and tries to access the counter, a member variable, without specifying the class name before.

    In short it is the issue of accessing classing member outside the class without declaring its object.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Problem with static and inline in C++

    As said before, static does not need to be again, when defining the method:

    Code:
      class MyClass {
        static int counter; 
        inline static int getCounter (); 
      }; 
      int MyClass: getCounter () {return counter;}
    Here the point is that the compiler is not obliged to follow the keyword 'inline' (unless you force the compiler to do so).

  4. #4
    Join Date
    Dec 2008
    Posts
    150

    Re: Problem with static and inline in C++

    Quote Originally Posted by kelfro View Post
    Here the point is that the compiler is not obliged to follow the keyword 'inline' (unless you force the compiler to do so).
    You mean the compiler is not forced to use the inline function call. The keyword "inline" is not used by it. It is used to perform several definitions of the same symbol in several translation units.

Similar Threads

  1. Windows -8 Release Preview Static IP Problem
    By sairohit123 in forum Operating Systems
    Replies: 1
    Last Post: 25-07-2012, 11:09 AM
  2. static noise problem in my microphone
    By Hollyn in forum Hardware Peripherals
    Replies: 6
    Last Post: 05-09-2011, 08:44 PM
  3. Java - static field problem
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 09:48 AM
  4. Static IP/Port Forward Problem
    By JAMIN in forum Networking & Security
    Replies: 3
    Last Post: 02-03-2009, 10:21 AM
  5. Problem with inline elements in XPath
    By Solitario in forum Software Development
    Replies: 2
    Last Post: 25-10-2008, 05:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,109,651.93415 seconds with 16 queries