CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2009
    Posts
    201

    Create a class? And load it?

    Derp.
    Last edited by realchamp; April 5th, 2012 at 12:01 PM.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Create a class? And load it?

    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++.
    Last edited by Mybowlcut; May 16th, 2009 at 06:38 PM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: Create a class? And load it?

    Well I've seen it in another scripting language. But I don't know if it is possible in 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.

    "Running a class" is something that makes no sense in C++. What it sounds like you want is functions.

  4. #4
    Join Date
    Feb 2009
    Posts
    201

    Re: Create a class? And load it?

    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.
    }

    Quote Originally Posted by Speedo View Post
    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.

    "Running a class" is something that makes no sense in C++. What it sounds like you want is functions.
    Thanks for your advice!

  5. #5
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Create a class? And load it?

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Create a class? And load it?

    Quote Originally Posted by Speedo View Post
    "Running a class" is something that makes no sense in C++. What it sounds like you want is functions.
    Well, it almost does.....you can "run" an object of a class if it has operator() defined. Boost.Thread uses this approach.

  7. #7
    Join Date
    Feb 2009
    Posts
    201

    Re: Create a class? And load it?

    Derp.
    Last edited by realchamp; April 5th, 2012 at 12:01 PM.

  8. #8
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Create a class? And load it?

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Create a class? And load it?

    1. You can use meta-programming which is what is happening with boost functions. You have a class that looks something like this:
    Code:
    class func
    {
    public:
       void operator() ();
    }
    Then you create an instance of func and run it like a function, eg
    Code:
    func f;
    f();
    And you can pass your instance of func around, passing it into a function etc.

    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().

    Code:
    class Task
    {
    public:
      virtual ~Task() {}
      virtual void run() = 0;
    };
    
    //...
    
    void run_task( Task & task )
    {
       task.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.

    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.

  10. #10
    Join Date
    Feb 2009
    Posts
    201

    Re: Create a class? And load it?

    Thank you SOOOOOOOOOOOOOOOOOOOOOOOOO much NMTop40!

    That was exactly(more likely) want I needed! Thanks ALOT!

    - Cheers,
    realchamp.

  11. #11
    Join Date
    Aug 2007
    Posts
    858

    Re: Create a class? And load it?

    Quote Originally Posted by realchamp View Post
    Thank you SOOOOOOOOOOOOOOOOOOOOOOOOO much NMTop40!

    That was exactly(more likely) want I needed! Thanks ALOT!
    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.

  12. #12
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Create a class? And load it?

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured