0

I read this good post (on undefined-reference-to-a-static-member), but i from what i can see i dont always required to defined the static member in the cpp. So i am asking for help to understand the rules.

several examples - all examples without def in the cpp. In the h file for class Master declaration is :

class Master{
public:
    static IDataSynchronization& sync_data_cb_;
}

In cpp:

void Master::start(IDataSynchronization& syncDataCB);   - error for undefined
void Master::start(int p,IDataSynchronization& syncDataCB);  no error

//Here is use in the static sync_data_cb_ void Master::sendData(){ list data = sync_data_cb_.syncData(); list::iterator it; for (it = data.begin(); it != data.end(); ++it) { sendto(instance_->data_sock_fd_, (*it).c_str(), (*it).length(), 0,(const struct sockaddr *) &instance_->target_host_data_, instance_->sockadd_length_);

    }
}

for this class:

class Logger {
public:
    static void Log(const char *format, ...);
private:
    static FILE* file_;
    static mutex mtx_;
};

If in another classes that i declared static static FILE* (and used it in the class methods) i dont getting an error for undefined it.

I am using in these static memners in all cases.

Can anyone clear the ruled for me?

Thank you

Community
  • 1
  • 1
Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
  • 1
    What do `Master::start` functions have to do with `sync_data_cb_` definition? And could it be that you don't use the `file_` variable? And do you mean compiling or compiling and linking? Because compiler doesn't really care because it doesn't really know if it's defined elsewhere. – Michael Krelin - hacker Oct 04 '12 at 14:15
  • The code `void Master::start(...);` lines won't even compile. Take the time to write fully functional code into the question. As long as you don't make clear what you need you won't get good answers. – David Rodríguez - dribeas Oct 04 '12 at 14:31

2 Answers2

4

The rules say you need a definition of syncDataCB if you use it. "If you use it" is defined differently formally, but this is close enough for understanding.

If you do use it without a definition, though, the compiler/linker isn't required to give an error. In practice that would mean that if you pass sync_data_cb_ to start, but don't use syncDataCB there, you won't get an error if start gets inlined, but will if it doesn't.

The fact that you won't always get an error message isn't relevant, it doesn't mean that the code is sometimes correct. If you use sync_data_cb_, you need a definition.

0

Like with many other things, only symbols that are actually used (odr-used in C++11 parlance) need to be defined.

You can have declarations for many more things that you need, and as long as they are not used you won't need a definition.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • @user1495181: You are not really showing the whole program, and there is a precise definition of what *use* means in the standard. The question is poorly stated, since the declarations are not clear (you are hiding the context -- class) and there are no *uses* shown there. At any rate, the standard is quite clear in that every symbol that is *odr-used* must be defined in the program. Post a minimal *complete* program and we can discuss it if you want. – David Rodríguez - dribeas Oct 04 '12 at 14:22
  • I don't believe that compiles **and links** without any definition in one of the translation units. That is an use of the member and thus the member must be *defined* in one of the translation units. – David Rodríguez - dribeas Oct 04 '12 at 14:30
  • I swear it is :) . I just did a demo . with argument before it and without argument before it. Any way i am going to always declare. Thanks a lot!!!! – Avihai Marchiano Oct 04 '12 at 14:33