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

T

timor.super

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.
 
R

red floyd

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?
 
T

timor.super

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.
 
V

Victor Bazarov

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
 
T

timor.super

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

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 ?
 
V

Victor Bazarov

[..]

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
 
T

timor.super

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.
 
I

Ian Collins

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 )
 
A

Alf P. Steinbach

* (e-mail address removed):
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 );
}
};
 
T

timor.super

* (e-mail address removed):












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.
 
E

Earl Purple

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.
 
W

W Karas

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);

?
 
T

timor.super

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,

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.
 
W

W Karas

On Feb 27, 12:34 pm, (e-mail address removed) wrote:

int myvar_1 = 1;
ing myvar_2 = 2;
int myvar[2] = { 1, 2 };

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top