Referencing a non-static class member in a static function [duplicate] - c++

This question already has answers here:
c++ calling a non static function from a static function
(3 answers)
Closed 8 years ago.
so I've got something like this:
//somewhere in the class header
static void bar();
Someobj baz;
//meanwhile in the implementation
void foo::bar()
{
baz.f()//this doesn't work b/c baz isn't declared as static
}
Do I have to declare everything I want to use in the static function foo as static also or is there a better way?

Yes you do. If you have 10 objects and they each have their own baz, then how is bar supposed to know which baz you're talking about?
It can't. That's why you can only have one baz as well, a static one.

Static functions are like global functions, there is no associated instance with them so you cannot refer to an instance member. If you need a single baz object associated with this class make it static also. Otherwise, you need to change your logic.

Related

What are the possible advantages of using the "static" directive outside of a class? [duplicate]

This question already has answers here:
The static keyword and its various uses in C++
(9 answers)
Closed 8 years ago.
I've been doing some experimenting with function libraries and namespaces. I noticed that you can declare a function in a namespace as static, although it is not within a class declaration:
hpp:
ANameSpace
{
static void aFunc();
};
cpp:
ANameSpace
{
static void aFunc()
{
std::cout<<"Static function called"<<std::endl;
}
};
I understand the concept of static class members and how they can be very useful, but are there any particular advantages to using static outside a class?
Note: I cast the final vote to close this question (can't delete because there is an answer) because the reference cited as dup, although it is much broader in scope, contains a detailed discussion of static. But I'm not sure if it exactly answers the question as I have rephrased it now.
A static global function is visible only in the scope of the file. This is probably due to C compatibility, where this was also possible.
This sums up every possible use of static pretty well.

setting static member variable inside a static method [duplicate]

This question already has answers here:
Undefined reference to static variable [duplicate]
(2 answers)
Closed 9 years ago.
I am beginner to C++ and have a doubt about static member variables and member functions.
I have implemented a class as follows -
class Foo
{
private:
static int myVariable;
public:
static void setMyVariable()
{
myVariable = 100;
}
static void resetMyVariable()
{
myVariable = 0;
}
};
There are following considerations when I wrote a code like that -
I want only one instance of class Foo. Thats why I made all member variables and functions as static.
I don't want the outside code to touch myVariable
I have put this class in a header file and included in my main file. When I do this, I get an error undefined reference to Foo::myVariable
I want to know if I can write a code which can satisfy above requirements?
Thanks !
You need to define static class variables somewhere:
e.g. in your main C++ file,
int Foo::myVariable;
Note that technically, by making everything static, you may have no instances of Foo.

how to define a function in the scope of a class but not as a member function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i do not mean friend function. I guess there is not a way to achieve this in C++ like any other languages. Am I right?
The background:
I have a helper function for a class A. I know there are a couple of options here
(1) to declare it in the same namespace as class A is in
(2) put it as a static function of class A.
I was just thinking if it is possible to do the following which is kind of the combination of the above two. i.e.,
I want that function to be right under the scope of A but not as a member function of class A.
I hope this is clearer now. Thanks a lot.
You mean a static member function.
class T
{
static void lol();
};
void T::lol()
{
/* ... */
}
int main()
{
T::lol();
}
This isn't really "in the scope of the class" any more than a free function would be, because the only thing that can really mean is access to member variables, and access to member variables requires an instance.
But this is the closest to what you've asked.
Do you mean like a static function?
class Foo
{
void memberFunction();
static void classFunction();
};
Foo foo;
foo.memberFunction(); // called as a member function
Foo::classFunction(); // called on the class, no object necessary
That's basically a function in the C++ "scope". I'm not sure what you mean by scope though. If you just mean namespace, then this is it.
You can declare it as a static function - that is a function that is defined against the class but which does not require access to any particular instance of that class's member variables:
static int foobar()...
Which is then called with
Foo::foobar();
If you want to limit the scope of the function so it can ONLY be called from inside the class, you can make it private much like any other member function.
If you wish, there's nothing stopping you from creating a standard member function and just not accessing any member variables...
The way I usually do this is to declare a function in the .c++ file with 'static' qualifier. Please realize that is different from the normal use of c++ 'static' functions.
If you declare in 'bar.c++' (not 'bar.h++'):
static void foo() {}
...it is a function which is not 'visible' outside its object file. I.e. a local helper function.
So to be precise:
bar.h++;
class bar
{
void f();
};
bar.c++;
#include "bar.h++"
static void foo() {}
void bar::f() { foo(); }
Cheers!
You can declare a nested class:
class A
{
class X
{
void helper(A object)
{
std::cout << object.member;
}
};
int member;
};
Here helper is a member function of class Y, not of class A. I am not sure how this helps you though.

callback function syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does void(U::*)(void) mean?
Considering the following:
template <class T>
class myButtoncb {
private:
T *ptr;
void (T::*cback) (void)
}
What I understand is:
void (*cback) (void)
Which is nothing but a function pointer that to a function that returns void, and takes no argument.
What I dont understand is, what is the importance of T::? Isn't it enough to declare
only like void (*cback) (void) ?
This says that it's a member function that has a this pointer. Otherwise, it would be a free function, wouldn't have any idea what object it was operating on, and wouldn't be able to access any non-static member functions or member variables.
From C++ FAQ
Is the type of "pointer-to-member-function" different from "pointer-to-function"?
Yep.
Link which I've provided to you has a lot of information about this topic.
The function, you pass there, must be declared inside the class T - the template parameter of myButtoncb. So you can use a function like the following:
class A
{
public:
void foo(void);
};
myButton<A> b;
b.cback = &A::foo;

Why does c++ class need to define static field(data member) outside the class scope? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Initializing private static members
Why I can't initialize non-const static member or static array in class?
It is strange to me. Why not assume there is a static field at the global scope?
It has to be placed somewhere (in some object file), so linker could find it. If you have declaration of class with static filed in .h file and include this file in a few .cpp files, then it would be ambiguous, which object file should have place allocated for this filed.
Please also note, that primitive type const static field could be initialized in class declaration:
class Foo
{
static const int n = 42;
};