0

Context

I am looking through a large C++ codebase which isn't mine, and I came across this problem

Problem

Suppose we have a class (AClass) in a header file:

double MethodName(anObject& obj, string s)

and that it is defined in the in the cxx file not as:

double AClass::AMethod(anObject& obj, string s){

as one would expect, but rather as:

double AMethod(anObject& obj, string s){

Without the namespace declaration. This is the only time in the whole cxx file when such a method is defined like this. There are no static declarations on the method, and AClass does not inherit from any other classes.

Also, I saw how it was called, (in a totally different file, besides AClass.h and AClass.cxx) and the line looks like this:

if(AMethod(obj1, str)>0){

My question is, how does this even compile and run. I would expect this code to falter when compiling

Research

This has been a hard one to search for. Either it is so basic, that it is not directly covered, or there is something complicated going on. If this is a simple straightforward question, please direct me to a topic I can search. I have looked through these:

Putting class declaration in .cpp file

Correct way to define C++ namespace methods in .cpp file

but they were not very pertinent. I have also tried Googling:

"c++ defining a method in cpp file"

"c++ no namespace when defining a method in cpp file"

etc, but to no avail.

NB

if you wish to downvote, I would appreciate it if you were to give a reason, otherwise, I will not learn. Also, if there is any more information, you require, I will do my best to fill you in

Thank You

Community
  • 1
  • 1
user7396627
  • 105
  • 2
  • 8
  • Could you provide links to the source files you are talking about? –  May 01 '17 at 02:56
  • Unfortunately not, they are proprietary. Is there any area of the code you would like to look at? – user7396627 May 01 '17 at 03:02
  • @qasimir, without a [mcve], it's difficult to offer sensible answers. – R Sahu May 01 '17 at 03:03
  • @RSahu I understand, but I am not sure what, in this case, is pertinent to include. I suspected that It would not be an easy one to either explain, or answer, given my level of understanding of this. I will see what else is there, and edit the question – user7396627 May 01 '17 at 03:07
  • @qasimir, my only suggestion will be to go through [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and create one. – R Sahu May 01 '17 at 04:14
  • So are you saying AMethod is declared in the header file inside the class and being implemented as a global function? – hugos May 01 '17 at 04:37

1 Answers1

0

For this declaration: double MethodName(anObject& obj, string s), you would not expect this: double AClass::AMethod(anObject& obj, string s). You'd expect this: double AClass::MethodName(anObject& obj, string s).

The call you are referring to AMethod(obj1, str)>0){ is only in the .cpp file and not in a header so it's a .cpp-local function; no namespace; it's a global function. This can happen so other classes that have to include the class don't have to compile this function. If this is the case, AMethod cannot use class members; it can only use data passed in as a variable.

ITISAGLN
  • 1
  • 5
  • I am under the impression that one provides return types in the header files, I have not seen it done otherwise. Sorry, I should have made clear, that it is used in other files completely different to the .cxx file. However, your answer makes sense, and has given me an idea... – user7396627 May 01 '17 at 04:07