Can somebody tell me what is the difference between static class and static local variable in C++ ?

u will be greatful if u explain it giving example

thanking you

Recommended Answers

C++ doesn't directly support a static class. Are you talking about a class that only has static members and doesn't have a public constructor?

Jump to Post

A static local variable retains its value between calls of the function. A static class has data that retains values between uses of the class and methods that act on the data. It's hard to compare the two because they're not really comparable. A static data member is like a …

Jump to Post

All 5 Replies

C++ doesn't directly support a static class. Are you talking about a class that only has static members and doesn't have a public constructor?

C++ doesn't directly support a static class. Are you talking about a class that only has static members and doesn't have a public constructor?

yes

A static local variable retains its value between calls of the function. A static class has data that retains values between uses of the class and methods that act on the data. It's hard to compare the two because they're not really comparable. A static data member is like a static local variable on steroids though. :)

static local variables and static data members have lifetimes like global variables (there is only one copy in the program, and it exists for the whole duration of the program), except that their scope is not global

> Are you talking about a class that only has static members and doesn't have a public constructor?
>> yes

that is the java style of programming. in C++, using a namespace for this purpose is canonical.

class foo
{
  foo() ;
  public:
   static int f() ;
   static double value ;
};
namespace foo
{
   int f() ;
   extern double value ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.