From: jamaj on
Hi all,

I'm writing a C/C++ program and i want to use a C written csv parser
lib within it. This routines are being called normally, and, for each
token, this parser calls a function. All is working ok, but i need to
use a static variable to do some adm. tasks, and i think I'm broking
the object-oriented paradigm.

I could rewrite the csv parser as a class, but i don't want. My
question is: is that terrible to use static variables in a mixed C/C++
program? Could anybody give me a suggestion to do it in a better and
elegant way?

Thanks in advance.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 30 Mrz., 04:22, jamaj <jama...(a)gmail.com> wrote:
> I'm writing a C/C++ program and i want to use a C written csv parser
> lib within it. This routines are being called normally, and, for each
> token, this parser calls a function. All is working ok, but i need to
> use a static variable to do some adm. tasks, and i think I'm broking
> the object-oriented paradigm.
>
> I could rewrite the csv parser as a class, but i don't want. My
> question is: is that terrible to use static variables in a mixed C/C++
> program? Could anybody give me a suggestion to do it in a better and
> elegant way?

First: If you really need to use a global variable, so
use it. I don't think that rewriting the whole stuff
as a class solves this particular problem (That is
IMO one of the misunderstandings of OO programming).

If there is a weakness in the approach of that library,
it would be the lack of a opportunity to provide a
user-defined context to your call-back. Many such
libraries allow this kind of provision and if they do
so, you can work in your usual "local" style, which is
a Good Thing. That it, checkout the API for some
way of setting a context (typically a void* parameter)
and check the signature of the callback for a parameter
where this context is returned for you to be available
within the call-back. If there is no way for a user-
defined context, it is worth contacting the person
responsible for this library to ask for such a support.

Rewriting code to use classes instead of free functions
doesn't solve problems that are related to lack of scope.
You can have a wonderful non-class C library, if this
library actively supports user-defined context.

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
jamaj wrote:
> I'm writing a C/C++ program

C/C++ is not a programming language. You should pick either of them and not
try to mix. That said, you can mix, which is even reasonable if you have a
codebase written in C.

> and i want to use a C written csv parser lib within it. This routines
> are being called normally, and, for each token, this parser calls a
> function. All is working ok, but i need to use a static variable to do
> some adm. tasks, and i think I'm broking the object-oriented paradigm.

Not using OOP is not a problem per se, OOP is a tool, not a goal. If it
doesn't help, don't use it.

> I could rewrite the csv parser as a class, but i don't want. My
> question is: is that terrible to use static variables in a mixed C/C++
> program? Could anybody give me a suggestion to do it in a better and
> elegant way?

You haven't said why you think you need to use a static variable anywhere,
so it's a bit hard to make a different suggestion.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]