1

I thought I cannot use a static pointer to call a non-static function, since I think a static pointer belongs to the whole class but non-static function belongs to a specific object. But somehow I made it succeed and am not sure why this is so.

Here is my code:

#include <cstdio>

class B
{
public:
    void show(int aValue);
};

//B.cpp
void B::show(int aValue)
{
    if (aValue == 100)
        printf("This is the desired value");
    else
        printf("This is not the desired value");
}

class A
{
private:
    static B* mUpdater;
public:
    static int function1();
};

B* A::mUpdater = new B();
int A::function1()
{
    int final = 100;
    mUpdater->show(final); // mUpdater is a static pointer, show is a non-static function
    return 1;
}


int main()
{
    return !A::function1();
}

The code runs, and prints "This is the desired value". But I am a little bit confused since I thought the code cannot run. Is this because I assign the address of a specific object to the pointer so that It can work? Or no matter in which case, static pointer can just call non-static functions?

Any idea will be more than appreciated.

6
  • Please provide more complete definitions of classes A and B. Commented Jan 28, 2016 at 15:19
  • 5
    The fact that mUpdater is static allows you to use A::mUpdater without an instance of A. Its usage afterward is identical. Commented Jan 28, 2016 at 15:20
  • In your example you don't call the show function of class A, but of mUpdater which is a pointer to an object of type B. Your question cannot be answered properly without a clear definition of class B. Commented Jan 28, 2016 at 15:29
  • well i had to remove two comments because I was a bit confused, please make it an MCVE. At the moment it is rather unclear. Does A inherit from B ? Commented Jan 28, 2016 at 15:29
  • 4
    I believe that your misunderstanding is that you "cannot use a static pointer to call a non-static function". What you can't do is call a non-static function if you don't have any object at all to call it "with" (that is, doing show(1); inside function1 won't work). As long as you have a valid object, how or where that object is stored doesn't matter. Commented Jan 28, 2016 at 15:37

3 Answers 3

2

(Let's assume that B inherits from A (see the comments to your question).)

A static member function means you cannot use the this pointer of the class/object. In A::function1() you do not use the this-pointer. Instead you call a function, which may or may not be static, of an actual instance (of B). It does not matter whether you access that instance via a static/non-static pointer/reference. Thus your usage is valid.

However, your code is quite confusing. Your show() function does not use any variables of the class anyway, so it might as well be static. Also it seems implausible that you use a pointer to another object of type B to call a non-static member function from a static member function of A. You might end up in a situation in which data of some instance of B is used when you expect data of some instance of A to be used.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your answer! I just simplified the code and I just wanna show the structure. Actually there are some other arguments in show function. I don't know why you think that B inherits from A, actually they are of no inherit relationship
Oh I know...Sorry I made a mistake, show() is of class B, I will modify my codes
1

I thought I cannot use a static pointer to call a non-static function, since I think a static pointer belongs to the whole class but non-static function belongs to a specific object.

The static member pointer belongs to the "whole" class, and so all instances (or other static member functions) can access it. But once they access it, they're accessing nothing more than a pointer.

And specifically, if the pointer is to an instance of that same class, then those functions can also access private members of the object pointed to by that pointer.

Comments

0

If I understand the question correctly, you are asking about

static B* mUpdater;

Specifically, you are surprised that

mUpdater->show(final);

is legal C++.

It's important to understand that the static keyword above refers to mUpdater, not what it points to. That is, there's a pointer, shared by all A that points to a single object of class B.

When you write mUpdater->show(), that dereferences A::mUpdater to access the single B object, and calls the show() method of that B.

You can simplify your example further, which makes it easier to pick out your question from the distractions around it:

#include <cstdio>

struct B
{
    void show() {
        printf("This is B::show()\n");
    }
};

struct A
{
    static B *worker;
    static void f() {
        worker->show();
    }
};

B* A::worker = new B();


int main()
{
    A::f();
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.