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

Discussion in 'C++' started by timor.super@gmail.com, Feb 27, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. red floyd Guest

    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?
     
    red floyd, Feb 27, 2007
    #2
    1. Advertising

  3. Guest

    On 27 fév, 18:51, red floyd <> wrote:
    > 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, 2007
    #3
  4. wrote:
    > On 27 fév, 18:51, red floyd <> wrote:
    >> 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
     
    Victor Bazarov, Feb 27, 2007
    #4
  5. Guest

    On 27 fév, 20:10, "Victor Bazarov" <> wrote:
    > wrote:
    > > On 27 fév, 18:51, red floyd <> wrote:
    > >> 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, 2007
    #5
  6. 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
     
    Victor Bazarov, Feb 27, 2007
    #6
  7. Guest

    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, 2007
    #7
  8. Ian Collins Guest

    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.
     
    Ian Collins, Feb 28, 2007
    #8
  9. * :
    > 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?
     
    Alf P. Steinbach, Feb 28, 2007
    #9
  10. Guest

    On 28 fév, 08:38, "Alf P. Steinbach" <> 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

    >
    > > 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, 2007
    #10
  11. Earl Purple Guest

    On 28 Feb, 08:42, wrote:
    > On 28 fév, 08:38, "Alf P. Steinbach" <> 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

    >
    > > > 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.
     
    Earl Purple, Feb 28, 2007
    #11
  12. W Karas Guest

    On Feb 27, 12:34 pm, 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);

    ?
     
    W Karas, Feb 28, 2007
    #12
  13. Guest

    On 28 fév, 13:02, "W Karas" <> wrote:
    > On Feb 27, 12:34 pm, 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, 2007
    #13
  14. JussiJ Guest

    On Feb 28, 7:42 pm, 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
     
    JussiJ, Mar 1, 2007
    #14
  15. W Karas Guest

    On Feb 28, 8:06 am, wrote:
    > On 28 fév, 13:02, "W Karas" <> wrote:
    >
    >
    >
    >
    >
    > > On Feb 27, 12:34 pm, 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.
     
    W Karas, Mar 1, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron
    Replies:
    1
    Views:
    2,708
    Showjumper
    Jun 24, 2003
  2. Ian
    Replies:
    0
    Views:
    1,390
  3. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    588
    Alphonse Giambrone
    Jun 28, 2003
  4. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,241
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  5. keithb
    Replies:
    1
    Views:
    916
    Bruce Barker
    Mar 29, 2006
Loading...

Share This Page