What is the meaning of returning a value of a static type

A

Anon

Hi,

As far as I know, a static variable is a variable that belongs to a function
or a class (are there any other options?). I've seen somewhere a function
that *returns* static types - something like:

static [const] someType myFunction() {...}

What is the meaning of it?? Or did I get something wrong?

Thanks!
 
A

Alf P. Steinbach

>As far as I know, a static variable is a variable that belongs to a function
or a class (are there any other options?).

It can also be at namespace/global scope.

I've seen somewhere a function
that *returns* static types - something like:

static [const] someType myFunction() {...}

What is the meaning of it?? Or did I get something wrong?

You did get something wrong.

In this context the word 'static' means "internal linkage" -- for
the function, not the return value.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

> As far as I know, a static variable is a variable that belongs to a function
or a class (are there any other options?). I've seen somewhere a function
that *returns* static types - something like:

static [const] someType myFunction() {...}

That static does not apply to the type of the return value, it applies
to the function, making it static.

static out of a function has a differente meaning, it marks his
parameter as non extern, that is, it's not seen from different units of
compilation.

Regards.
 
A

Ali R.

That's not what you think. That's a static member function. When you
declare a member function static, you will have access to the function
outside of the class, without instantiating an instance of the class. One
thing about this is that the function will not have access to any of the
none static members of the class. The Static member function is a very good
method for singleton classes. Try reading Effective C++, More Effective
C++, and Design Patterns.

Ali R.

class A
{
public:
static int DoSomething();
private:
int m_Counter;
static int m_StaticCounter;
}

int A::DoSomething()
{
m_StaticCounter++; //this is fine
m_Counter++; //error!!!
}
main()
{
A::DoSomething(); //this is also fine.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top