user800799
user800799

Reputation: 3013

How do I call local function from a static function?

#ifndef DATACENTER_H_
#define DATACENTER_H_

#include <map>
#include <list>
#include <string>

#include "LiLo/SoundInfo.h"
#include "MutexCondition.h"
#include "UserInfo.h"

using namespace std;

class DataCenter : MutexCondition{

private:

    map<long long, list<SoundInfo *> > m_soundListMap;


    void add(long long deviceId, SoundInfo* soundInfo);

public:

    DataCenter();
    virtual ~DataCenter();

    static void addSoundInfo(long long deviceId, SoundInfo *soundInfo);

};

#endif /* DATACENTER_H_ */

DataCenter.cpp file

#include "DataCenter.h"

DataCenter::DataCenter() {
    // TODO Auto-generated constructor stub

}

DataCenter::~DataCenter() {
    // TODO Auto-generated destructor stub
}

void DataCenter::addSoundInfo(long long deviceId, SoundInfo *soundInfo){
    add(deviceId, soundInfo);
}

void DataCenter::add(long long deviceId, SoundInfo *soundInfo){
    list<SoundInfo*>& info_list = m_soundListMap[55];
}

I am trying to access the function call addSoundInfo from other classes so I have set this as static. Since the m_soundListMap is not a static so I think I need another function to access to the local data structure.

Inside of the static function, I call add function to add SoundInfo to the list. However, I am getting an error in the static function and it says "Can not call member function .... without object".

How do I fix this problem? Thanks in advance..

Upvotes: 1

Views: 733

Answers (5)

Thanh DK
Thanh DK

Reputation: 4397

As others have said, making addSoundInfo() public is enough for it to be available from other class. I'll just add some points about C++'s keyword static. Basically, it has many meanings depending on where is it used. When one uses it for functions, there are two meanings:

  • static class function: a function that is tied to a class, not any specific object. In this sense, it is similar to namespace concept - using the scope :: operator to access the function.
  • static function: The function has internal linkage, which means it is only visible in current translation unit (current source file). It is handy for utility functions.

In your case, the answer to your question will technically be something like this:

In the header file:

class DataCenter 
{
    static void addSoundInfo(DataCenter& dc, long long deviceId, SoundInfo *soundInfo);
}

In the source file:

void DataCenter::addSoundInfo(DataCenter& dc, long long deviceId, SoundInfo *soundInfo)
{
    dc.add(deviceId, soundInfo);
}

But it is probably not what you want.

Upvotes: 0

PypeBros
PypeBros

Reputation: 2646

static is a way to instruct the compiler "the following function is not manipulating instance variable, only things that are global to all the instances of this class". You use that when you need to keep your constructor private for some reason, or have a function that does instances management (registration, etc.)

When you intend to have only one instance of a given class (e.g. because it is a resource manager), you usually prefer to follow the singleton pattern: a static getInstance() method that return the only instance of that class and create it if needed, then you keep your other methods regular methods and your state instance members.

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

The code seems hopelessly jumbled, but technically you just need to remove the word static. Then you can call dc.addSoundInfo( id, pSoundInfo ) where dc is a DataCenter object.

Cheers & hth.,

Upvotes: 0

Sasha
Sasha

Reputation: 856

I suppose you mean you don't want to make void add() public and still you want to access it from some classes. It is nothing wrong with that and you can do it this way:


class A
{
private:
   void DoPrivateStuf() {}

   friend class B;  // now B can access A private stuf
};

class B
{
// can have any modifier: public, private, protected depending on your needs
public:

  void DoPrivateStufToA( A& a )
  {
     a.DoPrivateStuf();
  }
};


Upvotes: 0

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76808

If you want to access addSoundInfo from other classes, you need to make it public, or make those other classes friends of DataCenter. static has nothing to with access control.

A static function is not bound to an instance of the class it belongs to, and thus can not access members of that class (it also can not call member-functions). If you really want to access members from a static function, you have to pass an instance of the class as argument the the static function explicitly.

If you struggle with such basic concepts, you should read a good book.

Upvotes: 4

Related Questions