I need some explanation about the following piece of code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: I need some explanation about the following piece of code

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    31

    I need some explanation about the following piece of code

    Hello,
    I am trying to understand the following code written in unix platform about unitTest:

    /****************************************/
    #define TEST(name,classUnderTest)
    class classUnderTest##name##Test : public Test
    {
    public:
    classUnderTest##name##Test () : Test (#name "Test") {}
    void setup() {};
    void teardown() {};
    void runTest (TestResult& result_);
    } classUnderTest##name##Instance;
    void classUnderTest##name##Test::runTest (TestResult& result_)

    /*****************************************************/

    Could anyone explain what does ##name## stand for? I would like to use in visual studio 2010.

    Please provide some suggestion as I am a newbie in writing unitTest.


    Thanks,

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Hello,
    I am trying to understand the following code written in unix platform about unitTest:
    Look up how to use the C++ preprocessor and token pasting.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2013
    Posts
    31

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by Paul McKenzie View Post
    Look up how to use the C++ preprocessor and token pasting.

    Regards,

    Paul McKenzie
    Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
    Code:
    #define TESTWITHSETUP(name,classUnderTest)
    	class classUnderTest##name##Test : public Test, name##Setup
    	{ 
    		public: 
    			classUnderTest##name##Test () : Test (#name "Test") {} 
                void setup() {name##Setup::setup();} 
                void teardown() {name##Setup::teardown();} 
    			void runTest (TestResult& result_); 
    	} classUnderTest##name##Instance; 
    	void classUnderTest##name##Test::runTest (TestResult& result_)

    In the following piece of code,

    "class classUnderTest##name##Test : public Test, name##Setup" , where name##Setup has not been define yet, how can it be used. Please give some clarification regarding this kind of syntax. I would like to run and compile some of this kind of code in visual studio 2010. It would be good, if I am referred to some tutorial website.

    Thanks.
    {

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,300

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    ... the following piece of code when I tried to paste in visual studio 2010, it showed error.
    Code:
    #define TESTWITHSETUP(name,classUnderTest)
    	...
    What is the error code and error message?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
    That is not a full program. You don't even show usage of the macro.
    Please give some clarification regarding this kind of syntax.
    Do you understand what "token pasting" is and what the C++ preprocessor handles token pasting? That is all the information you need to look up to figure out what ## is supposed to do.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
    From MSDN:
    Use line concatenation — place a backslash (\) immediately before the newline character — for long directives on multiple source lines.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Mar 2013
    Posts
    31

    Re: I need some explanation about the following piece of code

    Hi, I am familiar with C++, I went through the preprocessor and token pasting, I have a few questions in the light of the picture attached, it would cbe a great help in order to clarify my conception:

    1. “name##Setup” class has not yet been defined. How that class can be inherited by “class classUnderTest##name##Test”? Please give some clarification.
    2. What does the encircled line 2 (i.e. #name “Test”) mean?
    3. What does the encircled line 3 imply? (name##Setup::setup()) as name##Setup class has notyet been defined?
    4. At marking 4, “classUnderTest##name##Instance”, is it an instance of “classclassUnderTest##name##Test”, usually this kind of definition is used while writing stuct.
    5. In 5, the function “runTest” has again been defined, why?

    Thanks in advance.
    Attached Images Attached Images  

  8. #8
    Join Date
    Apr 1999
    Posts
    27,423

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Hi, I am familiar with C++, I went through the preprocessor and token pasting, I have a few questions in the light of the picture attached, it would cbe a great help in order to clarify my conception:
    You didn't circle the most important part of the whole thing, and that is the first line.

    Do you know what that #define does? What those parameters are? If you do, then all you need to do is see what happens if you invoked the macro with a sample line:
    Code:
    TESTWITHSETUP(name1, name2)
    Take that line, apply what you learned about token pasting, and expand that macro with those parameters. What do you ultimately end up with once the macro is expanded?

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,423

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    1. “name##Setup” class has not yet been defined. How that class can be inherited by “class classUnderTest##name##Test”? Please give some clarification.
    A macro need not have the class predefined. When the macro is expanded (something I asked you to do in the previous post, so that you see what it does), then the compiler will expect that the class is already defined, but not before. That macro is perfectly valid.

    Regards,

    Paul McKenzie

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center