Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

##

2 views
Skip to first unread message

nik

unread,
Aug 2, 2006, 10:37:41 AM8/2/06
to
hi friends,
plz tell me the use of ##
by giving example

mlimber

unread,
Aug 2, 2006, 10:41:12 AM8/2/06
to
nik wrote:
> plz tell me the use of ##
> by giving example

Plz experiment with google. It finds answers to such things easily. For
instance:

http://www.cppreference.com/preprocessor/sharp.html

Cheers! --M

Phlip

unread,
Aug 2, 2006, 10:43:10 AM8/2/06
to
nik wrote:

> plz tell me the use of ##
> by giving example

It's the "token pasting" operator. Use it to combine token fragments into a
complete identifier.

Here's a complete, useful example. It tests a WTL dialog box. (Fear not,
topic police, we see little Windows Template Library code here, and the
actual TEST_() macro itself is quite portable.)

I like the TEST_() macro better than the usual Test Suite system (where you

must write extra code to push each Test Case into its Suite's list), so I
wrote a CppUnit clone that uses _only_ the TEST_() macro.

Firstly, all TestCases obey the Abstract Template pattern:

class
TestCase
{
public:
virtual void setUp() {}
virtual void runCase() = 0;
virtual void tearDown() {}
};

Their caller will iterate a polymorphic list of cases, and call setUp(),
runCase(), and tearDown() on each one.

To test a WTL dialog box called ProjectDlg, with a name and address on it,
we write a Suite called TestDialog:

class
TestDialog: public TestCase
{
public:
ProjectDlg m_aDlg;

virtual void setUp()
{ m_aDlg.Create(HWND_TOP); }

virtual void tearDown()
{ m_aDlg.DestroyWindow(); }

TestDialog(): m_aDlg(xml) {}
};

Now we naively derive one concrete class for each of two test cases:

class
TestDialog_first_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Ignatz",
m_aDlg.getText(IDC_EDIT_FIRST_NAME) );
}
};

class
TestDialog_last_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}
};

Then we get this primordial test rig to pass, using an unrolled version of
the loop we will soon write:

int
main()
{
try {
TestDialog_first_name test1;
test1.setUp();
test1.runCase();
test1.tearDown();

TestDialog_last_name test2;
test2.setUp();
test2.runCase();
test2.tearDown();
}
catch(_com_error & e)
{
guard_(e);
ELIDE_(__asm { int 3 });
return 1;
}
return 0;
}

(Notice, despite I'm using that evil OLE COM ActiveX whatever library from
Microsoft, my code is not full of extra crud like CComBSTR or
CoCreateInstance(). Something chased it all away!)

From here, we need to upgrade TestCase et al to automatically collect the
Test Cases. They should not appear twice - once among the test suites and
again in main(). Removing behavioral duplication tends to improve code's
style.

Make each TestCase instance push itself into a static member list:

#include <list>

class
TestCase
{
public:
typedef std::list<TestCase *> TestCases_t;
TestCases_t static cases;

TestCase() { cases.push_back(this); }
virtual void setUp() {}
virtual void runCase() = 0;
virtual void tearDown() {}
};

TestCase::TestCases_t TestCase::cases;
...
class
TestDialog_last_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}
};
TestDialog_last_name test2;

(Remember that after each few edits all tests must still pass!) Now upgrade
main() to use the list:

int
main()
{
try {
TestCase::TestCases_t::iterator it (TestCase::cases.begin());

for ( ; it != TestCase::cases.end(); ++it )
{
TestCase & aCase = **it;
aCase.setUp();
aCase.runCase();
aCase.tearDown();
}
}

We continue to seek things that look similar, make them the same, and merge
their duplication.

The classes TestDialog_first_name (not shown) and TestDialog_last_name now
look very similar, but so do their object instances. To make them all the
same, we need to declare a class and instantiate an object in one command:

#define TEST_(suite, target) \
struct suite##target: public suite \
{ void runCase(); } \
a##suite##target; \
void suite##target::runCase()

TEST_(TestDialog, first_name)
{
CPPUNIT_ASSERT_EQUAL( "Ignatz",
m_aDlg.getText(IDC_EDIT_FIRST_NAME) );
}

TEST_(TestDialog, last_name)
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}

The object instances went inside the TEST_() macro, which uses
token##pasting to give them unique names. But they all must construct,
globally, so their constructors can register them with the list of cases.

This pattern makes Test Fixtures easy. Classes like TestDialog take care of
setting up and tearing down tested objects, providing a framework to share
other common operations on the tested objects. Test cases are now easy. The
Test Collector pattern ensures when we write new cases, we can't forget to
add them to a remote list of cases.

But these Test Cases no longer look like normal C++ functions!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


Phlip

unread,
Aug 2, 2006, 10:45:37 AM8/2/06
to
mlimber wrote:

> Plz experiment with google. It finds answers to such things easily. For
> instance:
>
> http://www.cppreference.com/preprocessor/sharp.html

Oh, a # is called a "sharp" today?

Wait 24 hours... ;-)

Rolf Magnus

unread,
Aug 2, 2006, 11:07:43 AM8/2/06
to
Phlip wrote:

> mlimber wrote:
>
>> Plz experiment with google. It finds answers to such things easily. For
>> instance:
>>
>> http://www.cppreference.com/preprocessor/sharp.html
>
> Oh, a # is called a "sharp" today?

Well, it looks similar, but actually isn't one.
See http://en.wikipedia.org/wiki/Number_sign

Phlip

unread,
Aug 2, 2006, 11:51:02 AM8/2/06
to
Rolf Magnus wrote:

>> Oh, a # is called a "sharp" today?
>
> Well, it looks similar, but actually isn't one.
> See http://en.wikipedia.org/wiki/Number_sign

What does the ISO C++ Standard call it?

Would Googling for that (if the OP could have figured out what to Google
for) possibly have worked?

Christopher Benson-Manica

unread,
Aug 2, 2006, 12:21:27 PM8/2/06
to
Phlip <phli...@yahoo.com> wrote:

> What does the ISO C++ Standard call it?

<ot>Microsoft has said the following concerning C#:

"The spoken name of the language is "C sharp" in reference to the
musical "sharp" sign, which increases a tone denoted by a letter
(between A and G) by half a tone. However, for ease of typing it was
decided to represent the sharp sign by a pound symbol [3] (which is on
any keyboard) rather than the "musically correct" Unicode sharp
sign. The Microsoft and ECMA 334 representation symbols thus agree:
the # in C# is the pound sign, but it represents a sharp sign. Think
of it in the same way as the <= glyph in C languages which is a less
than sign and an equals sign, but represents a less-than-or-equals
sign."</ot>

On a somewhat more topical note, the n869 draft of the C standard only
names the '#' once and calls it a "sharp sign".

One could always call it an "octothorpe" :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

Noah Roberts

unread,
Aug 2, 2006, 12:26:06 PM8/2/06
to

Phlip wrote:
> Rolf Magnus wrote:
>
> >> Oh, a # is called a "sharp" today?
> >
> > Well, it looks similar, but actually isn't one.
> > See http://en.wikipedia.org/wiki/Number_sign
>
> What does the ISO C++ Standard call it?

"The # operator"

It's actually an "octothorpe".

Phlip

unread,
Aug 2, 2006, 12:30:53 PM8/2/06
to
Noah Roberts wrote:

> "The # operator"
>
> It's actually an "octothorpe".

The great thing about Standards is there are so many to mislead your
competition...

Rolf Magnus

unread,
Aug 2, 2006, 12:58:02 PM8/2/06
to
Christopher Benson-Manica wrote:

> On a somewhat more topical note, the n869 draft of the C standard only
> names the '#' once and calls it a "sharp sign".

The final version does that too. The C++ standard doesn't even contain the
word "sharp" at all. AFAICS, it doesn't give that symbol any name at all.


Christopher Benson-Manica

unread,
Aug 2, 2006, 1:03:42 PM8/2/06
to
Rolf Magnus <rama...@t-online.de> wrote:

> The final version does that too. The C++ standard doesn't even contain the
> word "sharp" at all. AFAICS, it doesn't give that symbol any name at all.

That seems to have been a wise decision on the Committees' parts.

Marcus Kwok

unread,
Aug 2, 2006, 1:13:01 PM8/2/06
to
Christopher Benson-Manica <at...@otaku.freeshell.org> wrote:
> On a somewhat more topical note, the n869 draft of the C standard only
> names the '#' once and calls it a "sharp sign".
>
> One could always call it an "octothorpe" :-)

Sometimes I feel the need to call it "pound". As in, I pronounce
#include as "pound include".

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

Rat Monkey Hybrid

unread,
Aug 2, 2006, 1:31:37 PM8/2/06
to
Marcus Kwok wrote:
> Sometimes I feel the need to call it "pound". As in, I pronounce
> #include as "pound include".

As a Brit, I prefer to call it "hash" as in "hash include" or "hash
define". My American coworkers think I'm strange...

Victor Bazarov

unread,
Aug 2, 2006, 1:35:57 PM8/2/06
to

We are all strange for keeping coming back to "discuss" this nonsense.


Noah Roberts

unread,
Aug 2, 2006, 1:56:37 PM8/2/06
to

Rat Monkey Hybrid wrote:
> My American coworkers think I'm strange...

A logical conclusion, but not for the reason you seem to think.

Frederick Gotham

unread,
Aug 2, 2006, 3:30:04 PM8/2/06
to
Rat Monkey Hybrid posted:


I too say "hash include". In Europe, the pound sign is this:

http://www.afme.org.uk/res/img/resources/Pound.jpg

and it represents the currency of Britain.

--

Frederick Gotham

Sjouke Burry

unread,
Aug 2, 2006, 5:48:36 PM8/2/06
to
In Dutch: hekje include :)

Mike Smith

unread,
Aug 4, 2006, 11:59:52 AM8/4/06
to
Phlip wrote:
> mlimber wrote:
>
>> Plz experiment with google. It finds answers to such things easily. For
>> instance:
>>
>> http://www.cppreference.com/preprocessor/sharp.html
>
> Oh, a # is called a "sharp" today?
>
> Wait 24 hours... ;-)

OCTOTHORP.

--
Mike Smith

0 new messages