473,657 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating an object of a #defined class

Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP( testmapname) class testmapname\
{\
testmapname(){} \
testmapname(std ::ofstream 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(resf ile);

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 '06 #1
8 1898
na************* *@gmail.com 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.

Sep 1 '06 #2
na************* *@gmail.com wrote:
Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP( testmapname) class testmapname\
{\
testmapname(){} \
testmapname(std ::ofstream resultfile)\
This won't work. std::ofstream 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(resf ile);

then the compiler gives testmapname undefined and takes it as int.
Did you use the macro to define a class named 'testmapname'?

Sep 1 '06 #3
nandakumar.ragh u 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(resf ile);

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(resf ile); line into the macro, with \ before it.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Sep 1 '06 #4
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!!!
Sep 1 '06 #5
na************* *@gmail.com 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
Sep 1 '06 #6
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:
na************* *@gmail.com wrote:
Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP( testmapname) class testmapname\
{\
testmapname(){} \
testmapname(std ::ofstream resultfile)\

This won't work. std::ofstream 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(resf ile);

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

Did you use the macro to define a class named 'testmapname'?
Sep 2 '06 #7
Please don't top-post. Rearranged.

na************* *@gmail.com wrote:
Rolf Magnus wrote:
>na************* *@gmail.com wrote:
Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP( testmapname) class testmapname\
{\
testmapname(){} \
testmapname(std ::ofstream resultfile)\

This won't work. std::ofstream 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.

Sep 2 '06 #8

na************* *@gmail.com wrote:
Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP( testmapname) class testmapname\
{\
testmapname(){} \
testmapname(std ::ofstream 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(resf ile);

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 ::ofstream& ofile)\
{\
ofile << "<Test name=\"" << #testmapname <<
"\"\n" <<

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

int foo()
{
return 0;
}

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

std::ofstream x("a.txt");
a p(x);

return 0;
}

// note: do not put a ';' after BEGIN_TEST_MAP( a)

Sep 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2050
by: andthen | last post by:
If I have a Class object, serialized or somehow stored, is there any way I can use it to create a .class file?
30
2553
by: Sean R. Lynch | last post by:
I've been playing around with Zope's RestrictedPython, and I think I'm on the way to making the modifications necessary to create a capabilities-based restricted execution system. The idea is to strip out any part of RestrictedPython that's not necessary for doing capabilities and do all security using just capabilities. The basic idea behind capabilities is that you don't give any piece of code you don't trust a reference to something...
2
1531
by: JJ L. | last post by:
Hello. I have a project that consists of nine different objects, each serving their own purpose. In the past I have just created a form for each one, and then whenever you call, say, object.Display(), it would call up the form associated with that object. This form only displays information, it doesn't allow the user to edit any information. Is it possible to find the properties of a certain object, and then loop through creating labels...
4
1296
by: D | last post by:
I was reviewing these vb.net classes which deal with user logins, logouts, cookies etc etc. I noticed that in the Page_Load function of a user control the code calls into another library like so Call AnotherClassLibrary.AnotherClass.UserLogin(userId) I do not understand how this code can call this function without first creating an instance of the AnotherClass.
9
3871
by: MikeB | last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project for school and one of the requirements is that every screen should have a "Help" button. I could do it by writing a clumsy case statement like this: sub showHelp(byval frm as String) Select Case (frm) Case "Form1" dim help as new Form1
6
1782
by: wcc | last post by:
Hello, How do I create a class using a variable as the class name? For example, in the code below, I'd like replace the line class TestClass(object): with something like class eval(className) (object):
26
5354
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
1
5711
by: kaens | last post by:
So, I have a class that has to retrieve some data from either xml or an sql database. This isn't a problem, but I was thinking "hey, it would be cool if I could just not define the functions for say xml if I'm using sql", so I did some fiddling around with the interpreter. First, I try conditionally creating a function, period: a = 'a'
3
2277
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the ctors/dtors etc for brevity sake) struct MyStructA
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.