473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to do this in C++ (C code)

in C, I have this :

#define doStuff(var) func(var##_1, var##_2)

void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}

int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...
}

I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :

int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);

How can I do that ?

Thanks for your help,

S.

Feb 27 '07 #1
14 1372
ti*********@gmail.com wrote:
in C, I have this :

#define doStuff(var) func(var##_1, var##_2)

void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}

int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...
}

I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :

int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);

How can I do that ?
I wouldn't. It's a maintenance nightmare.
But if you want to, you do it the exact same way with the exact same
macro. The line c.doStuff(myvar) expands out to c.func(myvar_1, myvar_2)

What's the problem?
Feb 27 '07 #2
On 27 fév, 18:51, red floyd <no.s...@here.dudewrote:
timor.su...@gmail.com wrote:
in C, I have this :
#define doStuff(var) func(var##_1, var##_2)
void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}
int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...
}
I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :
int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);
How can I do that ?

I wouldn't. It's a maintenance nightmare.

But if you want to, you do it the exact same way with the exact same
macro. The line c.doStuff(myvar) expands out to c.func(myvar_1, myvar_2)

What's the problem?

the problem is that I don't know what to put as parameters ...

#define doStuff(var) func(var##_1, var##_2)
void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}

class MyClass
{
public:
MyClass();
void DoTheStuff(????) // <-- what should I put here ?
{
doStuff(theVar);
}
};

int _tmain(int argc, _TCHAR* argv[])
{
int myvar_1 = 1;
int myvar_2 = 2;
MyClass c;
c.DoTheStuff(myvar);
return 0;
}
with what can i replace the ???

I would like to have a proper solution, but you should know that I
can't modify the macro and I don't have access to the func code
function (this is an example test)

Thanks for your help

best,

S.

Feb 27 '07 #3
ti*********@gmail.com wrote:
On 27 fév, 18:51, red floyd <no.s...@here.dudewrote:
>timor.su...@gmail.com wrote:
>>in C, I have this :
>>#define doStuff(var) func(var##_1, var##_2)
>>void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}
>>int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...
}
>>I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :
>>int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);
>>How can I do that ?

I wouldn't. It's a maintenance nightmare.

But if you want to, you do it the exact same way with the exact same
macro. The line c.doStuff(myvar) expands out to c.func(myvar_1,
myvar_2)

What's the problem?


the problem is that I don't know what to put as parameters ...

#define doStuff(var) func(var##_1, var##_2)
void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}

class MyClass
{
public:
MyClass();
void DoTheStuff(????) // <-- what should I put here ?
{
doStuff(theVar);
}
Nothing. Just implement the 'func' here:

void func(int v, int v2) { ::func(v,v2); }
};

int _tmain(int argc, _TCHAR* argv[])
{
int myvar_1 = 1;
int myvar_2 = 2;
MyClass c;
c.DoTheStuff(myvar);
No, this should be

c.doStuff(myvar);
return 0;
}
with what can i replace the ???

I would like to have a proper solution, but you should know that I
can't modify the macro and I don't have access to the func code
function (this is an example test)
There is no "proper" solution. It's a BAD IDEA(tm) no matter how
you slice it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 27 '07 #4
On 27 fév, 20:10, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
timor.su...@gmail.com wrote:
On 27 fév, 18:51, red floyd <no.s...@here.dudewrote:
timor.su...@gmail.com wrote:
in C, I have this :
>#define doStuff(var) func(var##_1, var##_2)
>void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}
>int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...
}
>I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :
>int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);
>How can I do that ?
I wouldn't. It's a maintenance nightmare.
But if you want to, you do it the exact same way with the exact same
macro. The line c.doStuff(myvar) expands out to c.func(myvar_1,
myvar_2)
What's the problem?
the problem is that I don't know what to put as parameters ...
#define doStuff(var) func(var##_1, var##_2)
void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}
class MyClass
{
public:
MyClass();
void DoTheStuff(????) // <-- what should I put here ?
{
doStuff(theVar);
}

Nothing. Just implement the 'func' here:

void func(int v, int v2) { ::func(v,v2); }
};
int _tmain(int argc, _TCHAR* argv[])
{
int myvar_1 = 1;
int myvar_2 = 2;
MyClass c;
c.DoTheStuff(myvar);

No, this should be

c.doStuff(myvar);
return 0;
}
with what can i replace the ???
I would like to have a proper solution, but you should know that I
can't modify the macro and I don't have access to the func code
function (this is an example test)

There is no "proper" solution. It's a BAD IDEA(tm) no matter how
you slice it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
This is not what I want to do ...
of course, the macro is really more complex than a simple function
call. That's why I would like a solution where I can use parameter as
in my example.

Is this possible ?

Feb 27 '07 #5
ti*********@gmail.com wrote:
[..]

This is not what I want to do ...
What DO you want to do, then?
of course, the macro is really more complex than a simple function
call. That's why I would like a solution where I can use parameter as
in my example.

Is this possible ?
Is WHAT possible?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 27 '07 #6
ok, let me try to explain again :)

imagine that i've an old C style lib, that have many macro like that
(but very more complex) :

#define doStuff(var) {var##_1+=3; if(var##_2>10)
{var##_1+=5;}else{var##_1+=3;}}

I know that it is horrible, that's why I would like to make a C++
wrapper that can encapsulate the macro

today, the call to do stuff, is something like that :
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);

what I want to do is to achieve to have something like that, that
makes the same thing, with the quite same philosophy, but in object :

Myclass c(myvar);
c.doStuff();

note, that i don't want to do : Myclass c(myvar_1, myvar_2);
this is not possible, the macro are very more complex
Is there's a way in C++ to use the ##xxx like it's done in the macro,
in a constructor (or in a function) ?

I hope it's more clearer
Thanks for your help,

S.

Feb 28 '07 #7
ti*********@gmail.com wrote:
ok, let me try to explain again :)

imagine that i've an old C style lib, that have many macro like that
(but very more complex) :

#define doStuff(var) {var##_1+=3; if(var##_2>10)
{var##_1+=5;}else{var##_1+=3;}}

I know that it is horrible, that's why I would like to make a C++
wrapper that can encapsulate the macro
You can't encapsulate a macro, macros are expanded by the preprocessor,
not the compiler.

You could replace the body of the macro with an inline function, say

void realDoStuff( var1, var2 ) { ... }

#define doStuff(var) realDoStuff( var##_1, var##_2 )

--
Ian Collins.
Feb 28 '07 #8
* ti*********@gmail.com:
ok, let me try to explain again :)

imagine that i've an old C style lib, that have many macro like that
(but very more complex) :

#define doStuff(var) {var##_1+=3; if(var##_2>10)
{var##_1+=5;}else{var##_1+=3;}}

I know that it is horrible, that's why I would like to make a C++
wrapper that can encapsulate the macro

today, the call to do stuff, is something like that :
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);

what I want to do is to achieve to have something like that, that
makes the same thing, with the quite same philosophy, but in object :

Myclass c(myvar);
c.doStuff();

note, that i don't want to do : Myclass c(myvar_1, myvar_2);
this is not possible, the macro are very more complex
Is there's a way in C++ to use the ##xxx like it's done in the macro,
in a constructor (or in a function) ?

I hope it's more clearer
Thanks for your help,
class A
{
private:
int myVar1;
int myVar2;
public:
A( int v1, int v2 ): myVar1( v1 ), myVar2( v2 ) {}

void doStuff( int )
{
myVar1 += (myVar2 10? 8 : 6);
}
};

class B
{
private:
int myVar1;
int myVar2;
public:
B( int v1, int v2 ): myVar1( v1 ), myVar2( v2 ) {}

void doStuff( int )
{
DO_STUFF( myVar1, myVar 2 );
}
};
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 28 '07 #9
On 28 fév, 08:38, "Alf P. Steinbach" <a...@start.nowrote:
* timor.su...@gmail.com:
ok, let me try to explain again :)
imagine that i've an old C style lib, that have many macro like that
(but very more complex) :
#define doStuff(var) {var##_1+=3; if(var##_2>10)
{var##_1+=5;}else{var##_1+=3;}}
I know that it is horrible, that's why I would like to make a C++
wrapper that can encapsulate the macro
today, the call to do stuff, is something like that :
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
what I want to do is to achieve to have something like that, that
makes the same thing, with the quite same philosophy, but in object :
Myclass c(myvar);
c.doStuff();
note, that i don't want to do : Myclass c(myvar_1, myvar_2);
this is not possible, the macro are very more complex
Is there's a way in C++ to use the ##xxx like it's done in the macro,
in a constructor (or in a function) ?
I hope it's more clearer
Thanks for your help,

class A
{
private:
int myVar1;
int myVar2;
public:
A( int v1, int v2 ): myVar1( v1 ), myVar2( v2 ) {}

void doStuff( int )
{
myVar1 += (myVar2 10? 8 : 6);
}
};

class B
{
private:
int myVar1;
int myVar2;
public:
B( int v1, int v2 ): myVar1( v1 ), myVar2( v2 ) {}

void doStuff( int )
{
DO_STUFF( myVar1, myVar 2 );
}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

ok, thanks both for your answer.

if macro are expanded by the pre-processor, I can't do what I wanted
to do ... I will find another solution, as you proposed to me.

Best regards,

S.

Feb 28 '07 #10
On 28 Feb, 08:42, timor.su...@gmail.com wrote:
On 28 fév, 08:38, "Alf P. Steinbach" <a...@start.nowrote:


* timor.su...@gmail.com:
ok, let me try to explain again :)
imagine that i've an old C style lib, that have many macro like that
(but very more complex) :
#define doStuff(var) {var##_1+=3; if(var##_2>10)
{var##_1+=5;}else{var##_1+=3;}}
I know that it is horrible, that's why I would like to make a C++
wrapper that can encapsulate the macro
today, the call to do stuff, is something like that :
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
what I want to do is to achieve to have something like that, that
makes the same thing, with the quite same philosophy, but in object :
Myclass c(myvar);
c.doStuff();
note, that i don't want to do : Myclass c(myvar_1, myvar_2);
this is not possible, the macro are very more complex
Is there's a way in C++ to use the ##xxx like it's done in the macro,
in a constructor (or in a function) ?
I hope it's more clearer
Thanks for your help,
class A
{
private:
int myVar1;
int myVar2;
public:
A( int v1, int v2 ): myVar1( v1 ), myVar2( v2 ) {}
void doStuff( int )
{
myVar1 += (myVar2 10? 8 : 6);
}
};
class B
{
private:
int myVar1;
int myVar2;
public:
B( int v1, int v2 ): myVar1( v1 ), myVar2( v2 ) {}
void doStuff( int )
{
DO_STUFF( myVar1, myVar 2 );
}
};
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

ok, thanks both for your answer.

if macro are expanded by the pre-processor, I can't do what I wanted
to do ... I will find another solution, as you proposed to me.

Best regards,

S.- Hide quoted text -

- Show quoted text -
Redesign it all without macros.

It probably can be done but we don't know the model of what you are
trying to do.

Feb 28 '07 #11
On Feb 27, 12:34 pm, timor.su...@gmail.com wrote:
in C, I have this :

#define doStuff(var) func(var##_1, var##_2)

void func(int v, int v2)
{
printf("%d, %d\n", v, v2);

}

int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...

}

I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :

int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);

How can I do that ?

Thanks for your help,

S.
Why

int myvar_1 = 1;
ing myvar_2 = 2;

and not

int myvar[2] = { 1, 2 };

or

struct mytype {
int var_1, var2;
mytype(v1, v2) : var_1{v1}, var_2(v2) { }
};

mytype mystruct(1, 2);

?

Feb 28 '07 #12
On 28 fév, 13:02, "W Karas" <wka...@yahoo.comwrote:
On Feb 27, 12:34 pm, timor.su...@gmail.com wrote:
in C, I have this :
#define doStuff(var) func(var##_1, var##_2)
void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}
int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...
}
I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :
int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);
How can I do that ?
Thanks for your help,
S.

Why

int myvar_1 = 1;
ing myvar_2 = 2;

and not

int myvar[2] = { 1, 2 };

or

struct mytype {
int var_1, var2;
mytype(v1, v2) : var_1{v1}, var_2(v2) { }

};

mytype mystruct(1, 2);

?
Because it's only the test example, of course this is more complex,
and my purpose is to re-design it properly

Thanks for your advice.

Best,

S.

Feb 28 '07 #13
On Feb 28, 7:42 pm, timor.su...@gmail.com wrote:
if macro are expanded by the pre-processor, I can't do what
I wanted to do ... I will find another solution
My suggestion would be "if it aint broke, don't fix it!"

Jussi Jumppanen
Author: Zeus for Windows IDE
http://www.zeusedit.com

Mar 1 '07 #14
On Feb 28, 8:06 am, timor.su...@gmail.com wrote:
On 28 fév, 13:02, "W Karas" <wka...@yahoo.comwrote:


On Feb 27, 12:34 pm, timor.su...@gmail.com wrote:
in C, I have this :
#define doStuff(var) func(var##_1, var##_2)
void func(int v, int v2)
{
printf("%d, %d\n", v, v2);
}
int main()
{
int myvar_1 = 1;
int myvar_2 = 2;
doStuff(myvar);
// ...
}
I want to do a class to encapsulate the func, but how can I do the
same thing with a class ? I would like to do something like this :
int myvar_1 = 1;
int myvar_2 = 2;
CMyClass c;
c.doStuff(myvar);
How can I do that ?
Thanks for your help,
S.
Why
int myvar_1 = 1;
ing myvar_2 = 2;
and not
int myvar[2] = { 1, 2 };
or
struct mytype {
int var_1, var2;
mytype(v1, v2) : var_1{v1}, var_2(v2) { }
};
mytype mystruct(1, 2);
?

Because it's only the test example, of course this is more complex,
and my purpose is to re-design it properly

Thanks for your advice.

Best,

S.- Hide quoted text -

- Show quoted text -
There is a Boost (www.boost.org) preprocessor library,
which is amazingly capable given how bad the C/C++
preprocesor is as macro processors go.

If you can forgo the romance of all that pointless
heroism, the Unix m4 macro processor is much more capable.
I don't know if there is a GNU or otherwise highly
portable freeware version of m4.

Back in the 80s, I heard alot of complaints about
how using cpp or m4 in code was too confusing, because
macro calls look just like variables or function calls.
So I tried to write a macro processor with a syntax
that looked like the "macros" in makefiles. The C
source code is on my webpage:

http://www.geocities.com/wkaras

It's ok for small things, and could be made more
generally usable with a little work.

Mar 1 '07 #15

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

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
4
by: KenFehling | last post by:
Hello. I am wondering if there exists a piece of software that takes multiple .js files that are nicely indented and commented and create one big tightly packed .js file. I'm hoping the one file...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
7
by: blackrosezy | last post by:
#include char *code; void main() { char buf = "book";
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
1
by: ahammad | last post by:
Hello, I have written a fairly complex parsing tool that is used to parse information from company documents. The program works very well, but in order to insure that all the data is copied...
66
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...

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.