Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-05-2009
Member
 
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?
Reply With Quote
  #2  
Old 06-05-2009
Member
 
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.
Reply With Quote
  #3  
Old 06-05-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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).
Reply With Quote
  #4  
Old 06-05-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:11 AM.