Jump to content

[SOLVED] Insert function body in another file


photo

Recommended Posts

I've written a class with several functions. How can I define functions' bodies in another file ?

Class declaration is in *.h-file. I would like to create another file with class functions definitions.

For example, in C++ language - class declaration is in *.h-file, class definition is in *.cpp-file. How is it in Unigine script ?

Link to comment

Hi.

 

In Unigine script you can write function implementation in a declaration "*.h" file like this:

class Foo {
	void foo() {
		log.message(__FUNC__ + ": called\n");
	}
};

And you can write implementation separately from declaration like this:

class Foo {
	void foo();
};

Foo::foo() {
	log.message(__FUNC__ + ": called\n");
}

But there is no difference between *.cpp and *.h files in Unigine script. These extensions just a formality.

 

See more at https://developer.unigine.com/en/docs/1.0/scripting/language/preprocessor#include

Link to comment

Thank you for quick answer.

 

Yes, I can write declaration and definition separatelly in one file.

But can I write them in two separate files ?

Link to comment

Hi. You can include implementation by simple #include:

// test.h file

class Foo {
	void foo();
};

// test.cpp file
#include <test/test.h>

void Foo::foo() {
	log.message(__FUNC__ + ": called\n");
}

//main.cpp file

#include <test/test.cpp>

int init() {
	Foo f = new Foo();
	f.foo();
	return 1;
}
Link to comment

Thank you very much!

 

I did all the same before, but I include test.h in main.cpp (and don't include test.cpp) - because I thought in C/C++ style.

After I've included test.cpp in main.cpp - all works correct.

 

Now I understand your words:

But there is no difference between *.cpp and *.h files in Unigine script. These extensions just a formality.

Link to comment
×
×
  • Create New...