Derp.
Printable View
Derp.
Hmmm no, that's not possible. For starters, test would have to be declared first - as it is now, the compiler will have no idea what test is. Secondly, do test isn't a legal statement. I think you're trying to create a test object instead?
What are you trying to gain by using a class where a function or even just a plain old cout call would suffice? If we know what you're trying to do (and why you want a class to do it), we can offer you a suggestion to do it another way that is legal C++.
Do yourself a favor and don't treat C++ like whatever other language you know, and especially don't try to make it do things the way they were done in that language.Quote:
Well I've seen it in another scripting language. But I don't know if it is possible in C++?
"Running a class" is something that makes no sense in C++. What it sounds like you want is functions.
Okay. Then I'd ratherly have a function.
Well I have a program, in that program I have alot amount of text. And I'd like to have that text out of the int main().
So by doing that class(function) I should be possible to load it, when it's necessary. I don't really know a particulary reason for why I want it(performance etc). However I'd be much easier for me to hold on too. And looks better ;)
I've seen it in other source-codes. But I didn't really get the concept of it.
But wouldn't a void test work? Like so:
Code:// HEADERS..
using namespace std;
void test;
int main()
{
string A;
cout << "Hello world!!!\n";
cout << "Enter A\n";
cin >> A;
// If it's true, it should run the test class
if(A == A) do test
// Instead of "do" what should I then use?
}
function test {
cout << "something here\n";
some more code here... etc. etc. etc.
}
Thanks for your advice!
What programming languages are you familiar with? I ask because it seems as if you don't know how to define a function in C++, which is fairly fundamental knowledge.
Derp.
So it's not C++ evidently (I haven't looked at the link, but I don't really need to). Depending on how serious you are about getting this program to work, I suggest you either buy a C++ book or read some tutorials online.
Good luck.
1. You can use meta-programming which is what is happening with boost functions. You have a class that looks something like this:
Then you create an instance of func and run it like a function, egCode:class func
{
public:
void operator() ();
}
And you can pass your instance of func around, passing it into a function etc.Code:func f;
f();
Because of the way C++ is a strongly-typed language, when you pass this around, it must be known at compile time the type of the function. If you want run-time polymorphism then you need instead to create an abstract class. You can make operator() virtual but often it will be a named function like run().
And you would pass into run_task a class derived from Task, so what actually runs here would only be known at run-time. Thus different implementations could be passed through at different times dependent on run-time conditions. Of course you can also change the return value of Task and add in parameters to the function, although there is often no need for parameters, because you will just make those class members.Code:class Task
{
public:
virtual ~Task() {}
virtual void run() = 0;
};
//...
void run_task( Task & task )
{
task.run();
}
You can make run() a const function if you know it will never change the state of the input class.
It could be that the outer shell (that calls run()) runs asynchronously, i.e. it creates a new thread and runs the task. You might also use smart-pointers to pass in the parameter.
Thank you SOOOOOOOOOOOOOOOOOOOOOOOOO much NMTop40!
That was exactly(more likely) want I needed! Thanks ALOT!
- Cheers,
realchamp.
Please, for the love of god, DON'T try to dive into the stuff that NMTop40 showed you. You need to learn to learn the basics of the language from the ground up and the type of thing that he's talking about is definetly WELL above your current knowledge level. It's like trying to walk into a calculus class when you don't know how to do 2+2.
Or walking into a calculus class when you're supposed to be going to English... or perhaps even more like using a sledge hammer to turn off a light switch, since a function is really all you need. :)