Sign in to follow this  
Agwan

C++ Class static methods sharing

Recommended Posts

Agwan    122
Hello I've just started to learn C++ to see what it's like, I usually use C#. The following code generates Error an identifier not found error. I think the solution is a header file, but i am not familiar with the concept or how to fix the problem. In One File
namespace MySpace
{
	
	class MyClass
	{
	public:
		static void MyMethod(something.. )
		{
		       //Logic
		}
	};

}

Somewhere in another file

MySpace::MyClass::MyMethod(params.. );


Share this post


Link to post
Share on other sites
Captain P    1092
Your class and function are not known in that other file unless you make it known. You can do so by putting the definition in a header file (while keeping the implementation in a cpp file) and including that header wherever you need to use that class.

For example:
test.h
// Definition:
#ifndef TEST_H
#define TEST_H
class Test
{
public:
int Function();
};
#endif
test.cpp
// Implementation:
#include "test.h"

int Test::Function()
{
return 5;
}
otherfile.cpp
// Usage:
#include "test.h"

int main()
{
Test a;
a.Function();
}

Note that you can put your implementation in a header file, but that's generally avoided since changing a header means you'll have to recompile any cpp file that includes it. Only for inline functions or template classes this is done. What it comes down to is that a header file contains the 'interface' of your class. The function names and signatures. That's what other code needs to know if it wants to call your functions and use your classes.

Share this post


Link to post
Share on other sites
hh10k    589
You'll also want to look into include guards. The C/C++ preprocessor does make you work harder to implement what was easy in C#, but there are some benefits that come from the flexibility.

Share this post


Link to post
Share on other sites
Agwan    122
OK,

Is there any method of grouping the class contents if the class is defined in the header?

If not for any other reason than being able to collapse the code block in Visual Studio.

Share this post


Link to post
Share on other sites
Gage64    1235
Quote:
Original post by Agwan
OK,

Is there any method of grouping the class contents if the class is defined in the header?

If not for any other reason than being able to collapse the code block in Visual Studio.


If you're talking about something like the #region/#endregion directives from C#, in VC++ you can use #pragma region/endregion, but it might not be supported on other compilers.

Share this post


Link to post
Share on other sites
Agwan    122
No, that's now quite what I meant; I was looking for something more like the syntax for partial classes .. ish, thanks anyway.

Share this post


Link to post
Share on other sites
Captain P    1092
It doesn't really matter where you put the implementation of a class - this can be spread across multiple files if you so desire. You can't, however, split the definition across multiple files.

You probably don't want to, either - if your class definitions grow that large, you're likely focusing too much responsibilities in one place.


Or did you mean something else?

Share this post


Link to post
Share on other sites
Agwan    122
Not Quite, I mean like this

HEADER FILE

namespace MySpace
{
definetypething class MyClass
{
public:
static void MyMethod(something.. );
};
}



SOURCE FILE

namespace MySpace
{
definetypething class MyClass
{
public:
static void MyMethod(something.. );
{
//Logic and actual stuff
}
};
}




[Edited by - Agwan on August 18, 2008 9:15:49 PM]

Share this post


Link to post
Share on other sites
Captain P    1092
The header file looks fine (except for 'definetypething'). The source file needs to be modified somewhat, though. Keep in mind that you're working with a different language now, one that uses a much older compilation system.

The following code should do the trick. For every function that you provide an implementation, you'll have to specify it's 'origin'. That is, to what class it belongs. If you wrote a class definition around it, you'd be defining that class again and that's not going to work.

// Source file: myclass.cpp
#include "myclass.h"

namespace MySpace
{
void MyClass::MyMethod(something.. )
{
//Logic and actual stuff
}
}


Note that, for static functions, you may as well use loose functions: unlike C#, C++ does not force you to use classes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this