creating an object of a #defined class

Discussion in 'C++' started by nandakumar.raghu@gmail.com, Sep 1, 2006.

  1. Guest

    Hi,

    I am creating a #defined class like -

    #define BEGIN_TEST_MAP(testmapname) class testmapname\
    {\
    testmapname(){}\
    testmapname(std::eek:fstream resultfile)\
    {\
    resultfile << "<Test name=\"" << #testmapname << "\"\n";
    #define TEST_CASE(_test_case)\
    resultfile << _test_case();
    #define END_TEST_MAP \
    resultfile << "</Test>";\
    }\
    };\

    Now I want to create a global object for this class. How can I do this?
    When I write

    testmapname objectname(resfile);

    then the compiler gives testmapname undefined and takes it as int.

    so how do i create an object of this class. I only need to call the
    constructor of this class.


    Thanks

    Nandakumar
    , Sep 1, 2006
    #1
    1. Advertising

  2. Bart Guest

    wrote:
    > I am creating a #defined class like -


    <snip really ugly macro>

    > Now I want to create a global object for this class. How can I do this?


    Why are you doing this? Have you considered using a template instead?

    Regards,
    Bart.
    Bart, Sep 1, 2006
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > Hi,
    >
    > I am creating a #defined class like -
    >
    > #define BEGIN_TEST_MAP(testmapname) class testmapname\
    > {\
    > testmapname(){}\
    > testmapname(std::eek:fstream resultfile)\


    This won't work. std::eek:fstream is not copyable.

    > {\
    > resultfile << "<Test name=\"" << #testmapname << "\"\n";
    > #define TEST_CASE(_test_case)\
    > resultfile << _test_case();
    > #define END_TEST_MAP \
    > resultfile << "</Test>";\
    > }\
    > };\
    >
    > Now I want to create a global object for this class. How can I do this?


    What class? You only have a macro up to this point. You first have to use it
    to define a class.

    > When I write
    >
    > testmapname objectname(resfile);
    >
    > then the compiler gives testmapname undefined and takes it as int.


    Did you use the macro to define a class named 'testmapname'?
    Rolf Magnus, Sep 1, 2006
    #3
  4. Phlip Guest

    nandakumar.raghu wrote:

    > I am creating a #defined class like -
    >
    > #define BEGIN_TEST_MAP(testmapname) class testmapname\

    ....
    > Now I want to create a global object for this class. How can I do this?
    > When I write
    >
    > testmapname objectname(resfile);
    >
    > then the compiler gives testmapname undefined and takes it as int.
    >
    > so how do i create an object of this class. I only need to call the
    > constructor of this class.


    The CppUnit source code shows all this in action. (Further, one should use a
    much lighter test rig, such as UnitTest++, with a simple suite system.)

    Given BEGIN_TEST_MAP(Foo), the class name is Foo. Either use that, or put
    your testmapname objectname(resfile); line into the macro, with \ before it.

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
    Phlip, Sep 1, 2006
    #4
  5. Phlip Guest

    Bart wrote:

    > Why are you doing this?


    It's a common system to implement the Test Collector Pattern in a unit test
    rig.

    (All programmers write unit tests, so I must assume you are simply
    unfamiliar with the pattern.)

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
    Phlip, Sep 1, 2006
    #5
  6. posted:

    > Now I want to create a global object for this class. How can I do this?



    You don't understand macros. A macro is simple text replacement _before
    anything is compiled_.

    Perform the text replacement manually yourself and you'll see any and all
    error.

    --

    Frederick Gotham
    Frederick Gotham, Sep 1, 2006
    #6
  7. Guest

    Yes, i am writing this macro to define a class for a particular test.
    This way each test will have its own class when the macros for
    BEGIN_TEST_MAP will be expanded. Now I need to call the constructor of
    each class which actually executes the testcase _test_case();
    So how do i create an object.

    Writing testmapname objname; does not work as testmapname is not
    defined beyond BEGIN_TEST_MAP.

    Rolf Magnus wrote:
    > wrote:
    >
    > > Hi,
    > >
    > > I am creating a #defined class like -
    > >
    > > #define BEGIN_TEST_MAP(testmapname) class testmapname\
    > > {\
    > > testmapname(){}\
    > > testmapname(std::eek:fstream resultfile)\

    >
    > This won't work. std::eek:fstream is not copyable.
    >
    > > {\
    > > resultfile << "<Test name=\"" << #testmapname << "\"\n";
    > > #define TEST_CASE(_test_case)\
    > > resultfile << _test_case();
    > > #define END_TEST_MAP \
    > > resultfile << "</Test>";\
    > > }\
    > > };\
    > >
    > > Now I want to create a global object for this class. How can I do this?

    >
    > What class? You only have a macro up to this point. You first have to use it
    > to define a class.
    >
    > > When I write
    > >
    > > testmapname objectname(resfile);
    > >
    > > then the compiler gives testmapname undefined and takes it as int.

    >
    > Did you use the macro to define a class named 'testmapname'?
    , Sep 2, 2006
    #7
  8. Rolf Magnus Guest

    Please don't top-post. Rearranged.

    wrote:

    > Rolf Magnus wrote:
    >> wrote:
    >>
    >> > Hi,
    >> >
    >> > I am creating a #defined class like -
    >> >
    >> > #define BEGIN_TEST_MAP(testmapname) class testmapname\
    >> > {\
    >> > testmapname(){}\
    >> > testmapname(std::eek:fstream resultfile)\

    >>
    >> This won't work. std::eek:fstream is not copyable.
    >>
    >> > {\
    >> > resultfile << "<Test name=\"" << #testmapname << "\"\n";
    >> > #define TEST_CASE(_test_case)\
    >> > resultfile << _test_case();
    >> > #define END_TEST_MAP \
    >> > resultfile << "</Test>";\
    >> > }\
    >> > };\
    >> >
    >> > Now I want to create a global object for this class. How can I do this?

    >>
    >> What class? You only have a macro up to this point. You first have to use
    >> it to define a class.

    >
    > Yes, i am writing this macro to define a class for a particular test.
    > This way each test will have its own class when the macros for
    > BEGIN_TEST_MAP will be expanded. Now I need to call the constructor of
    > each class which actually executes the testcase _test_case();
    > So how do i create an object.
    >
    > Writing testmapname objname; does not work as testmapname is not
    > defined beyond BEGIN_TEST_MAP.


    It is not defined at all. As I said, there is no class. The code just
    defines a macro, nothing more. testmapname is a parameter to that macro and
    has no meaning outside it. If you use the macro to define a class, you
    specify the class's name as an argument to the macro. If you want that
    class instantiated, you have to use that name.
    Rolf Magnus, Sep 2, 2006
    #8
  9. Peyman Guest

    wrote:
    > Hi,
    >
    > I am creating a #defined class like -
    >
    > #define BEGIN_TEST_MAP(testmapname) class testmapname\
    > {\
    > testmapname(){}\
    > testmapname(std::eek:fstream resultfile)\
    > {\
    > resultfile << "<Test name=\"" << #testmapname << "\"\n";
    > #define TEST_CASE(_test_case)\
    > resultfile << _test_case();
    > #define END_TEST_MAP \
    > resultfile << "</Test>";\
    > }\
    > };\
    >
    > Now I want to create a global object for this class. How can I do this?
    > When I write
    >
    > testmapname objectname(resfile);
    >
    > then the compiler gives testmapname undefined and takes it as int.
    >
    > so how do i create an object of this class. I only need to call the
    > constructor of this class.
    >
    >
    > Thanks
    >
    > Nandakumar


    Here is the whole code you need!
    #include <iostream>
    #include <fstream>

    #define BEGIN_TEST_MAP(testmapname) class testmapname\
    {\
    public:\
    testmapname()\
    {\
    std::cout << "just working!\n";\
    }\
    testmapname(std::eek:fstream& ofile)\
    {\
    ofile << "<Test name=\"" << #testmapname <<
    "\"\n" <<

    #define TEST_CASE(output)\
    output() << "<\\Test>";\
    }\
    };

    int foo()
    {
    return 0;
    }

    int main()
    {
    BEGIN_TEST_MAP(a)
    TEST_CASE(foo);

    std::eek:fstream x("a.txt");
    a p(x);

    return 0;
    }

    // note: do not put a ';' after BEGIN_TEST_MAP(a)
    Peyman, Sep 3, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web Team @ Borough of Poole

    Creating user defined object

    Web Team @ Borough of Poole, Apr 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    387
    Kevin Spencer
    Apr 18, 2005
  2. Nathan Sokalski
    Replies:
    1
    Views:
    2,517
    Gaurav Vaish \(www.EdujiniOnline.com\)
    Nov 21, 2006
  3. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  4. Angel Tsankov
    Replies:
    1
    Views:
    864
    Victor Bazarov
    Apr 5, 2006
  5. Paul Brannan
    Replies:
    13
    Views:
    293
    Paul Brannan
    Mar 4, 2008
Loading...

Share This Page