static member functions

This is a discussion on static member functions within the C++ Programming forums, part of the General Programming Boards category; This is sort of a dumb question but I've been wondering about it for many months. I wondering if a ...

  1. #1
    fsp
    fsp is offline
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    static member functions

    This is sort of a dumb question but I've been wondering about it for many months. I wondering if a class's static member function is equivelent to a global function but in a different namespace. Can a class be effectively called a namespace?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can a class be effectively called a namespace?
    Yes, more or less.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Can a class be effectively called a namespace?
    My impression is: no. A namespace just adds a qualifying name to a function or variable, and you can call the function or variable at any time using the full name:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    namespace Apple
    {
    	int num = 10;
    }
    
    int main()
    {
    	cout<<Apple::num<<endl;
    
    	return 0;
    }
    Normally, class functions or variables can only be called by class objects, but a public static function or variable in a class is an exception to that rule, and it is called with a similar syntax as when using a namespace:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class Apple
    {
    public:
    	static int num;
    };
    
    int Apple::num = 10; //initialize the static variable
    
    int main()
    {
    	cout<<Apple::num<<endl; 
    	
    	return 0;
    }
    However, in general static functions and variables seem very different from a function or a variable in a namespace. For instance, a static variable can have a private access specifier, which limits access to the variable--only class objects that call class methods can access the variable.
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class Apple
    {
    private:
    	static int num;
    
    public:
    	void display()
    	{
    		cout<<num<<endl;
    	}
    
    	void set_num(int n)
    	{
    		num = n;
    	}
    
    };
    
    int Apple::num = 10; //initialize the static variable
    
    int main()
    {
    	Apple myApple;
    	myApple.display();
    
    	cout<<Apple::num<<endl;  //error: access denied
    
    	return 0;
    }
    Variables in namespaces don't have access specifiers.

    In addition, every object of a class has a pointer to the static variable, so when you change a static variable it is reflected in all the objects of the class:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class Apple
    {
    public:
    	static int num;
    };
    
    int Apple::num = 10; //initialize the static variable
    
    int main()
    {
    	cout<<Apple::num<<endl;
    	
    	Apple apple1, apple2;
    
    	cout<<apple1.num<<" "<<apple2.num<<endl;
    	Apple::num = 40;
    	cout<<apple1.num<<" "<<apple2.num<<endl;
    
    	return 0;
    }
    A namespace variable has no comparable functionality.

    Namespaces allow you to declare using directives so you can forgo having to use the qualifying namespace name, which you can't do with a static class variable or function.

    edit:
    Quote Originally Posted by Prelude
    >Can a class be effectively called a namespace?
    Yes, more or less.
    Uh, oh.

    Quote Originally Posted by Prelude
    Do not post below this line
    ---------------------------------
    For senior member use only
    ...wish I had been paying attention.
    Last edited by 7stud; 04-04-2005 at 03:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Static member func clone?
    By DV64h in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2007, 09:08 PM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21