1

Possible Duplicate:
Code convert from C++ to C
Converting a C++ class to a C struct (and beyond)

I used to be a C++ programmer, but now I need to write a program in C. e.g. in C++

Main.cpp
=====================
int main
{
  ns::Sum sum(1, 2);
}

Sum.h
=====================
namespace ns
{
  class Sum
  {
    public:
    Sum(int a, int b);
    private:
    void AddThemUp();
    int a;
    int b;
  }
}

Sum.cpp
======================
namespace ns
{
  Sum::Sum(int a, int b)
  {
    this->a = a;
    this->b = b;
    AddThemUp();
  }

  void Sum::AddThemUp()
  {
     a + b;//stupid for returning nothing, just for instance
  }
}

That's in C++ I don't know how to put above in C. because there is no class there. if I declare data member a & b in header file, they will become global variables. I don't like global variables. and is there namespace in C? who can help? thank you

Community
  • 1
  • 1

4 Answers4

3

Here is a simple conversion from C++ to C. This method allows for stack objects, like how you use ns::Sum. As a note, you should also have a release function that cleans up any memory allocated by the structure.

// Main.c
// =====================

// Construction
ns_Sum sum;
ns_SumInitWithAAndB(&sum, 1, 2);

// Sum.h
// =====================

// All member variables are contained within a struct
typedef struct ns_Sum_
{
    int a;
    int b;
} ns_Sum;

// Public Methods
void ns_SumInitWithAAndB(ns_Sum *sum, int a, int b);

// Sum.c
// ======================

// Private Methods can be declared as static functions within the .c file
static void ns_SumAddThemUp(ns_Sum *sum);

void ns_SumInitWithAAndB(ns_Sum *sum, int a, int b)
{
    sum->a = a;
    sum->b = b;
    ns_SumAddThemUp(sum);
}

void ns_SumAddThemUp(ns_Sum *sum)
{
    a + b; //stupid for returning nothing, just for instance
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • You mean I can use a struct to store all the data members. And make it a parameter to some functions? – MPvaliantboy Sep 22 '12 at 21:00
  • I dont know the meaning of keyword static here. not the same as C++? – MPvaliantboy Sep 22 '12 at 21:02
  • @MPvaliantboy Yes, you can use structs to store data members. Static functions are only available to the current C source file, extern functions (the default) are made available to any C source file in the final linked binary. – Jeffery Thomas Sep 23 '12 at 12:19
0

No, that is one of the limitations of C, you do not have separate namespaces built into the language. This is usually done by adding prefixes to names. As for global variables, you can use the extern storage specifier to declare but not define them.

Header:

#pragma once

extern int ns_foo;

Souce:

#include "header.h"

int ns_foo = 0;
bitmask
  • 32,434
  • 14
  • 99
  • 159
0

The conventional approach to use certain features which C++ gives you in C++ has to do with a lot of manual work:

  • Instead of using namespace, you'd use prefixes for your structs and functions, e.g., struct ns_Sum and void ns_Sum_AddThemUp().
  • C doesn't have member functions. So, instead of calling function on an object, you'd rather pass the object as a first parameter, e.g., void ns_Sum_SumThemUp(ns_Sum* object).
  • The way to make members private is to only declare the struct in the header and define it in the source. This goes together with suitable create and destroy function:

That is the declarations in a C header could look something like this:

typedef struct ns_Sum ns_Sum;
ns_Sum* ns_Sum_Create();
void    ns_Sum_Destroy(ns_Sum*);
void    ns_Sum_AddThemUp(ns_Sum const*);

For simple structures using a mapping like this works reasonable well. It becomes a Royal pain in the some-body-part-goes-here to also implement polymorphic dispatch or to deal with the moral equivalent of templates. My personal view of using C++ in C is to use C++ but I realize that this preference isn't shared with everybody...

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

Others already have pointed out how to use a name prefix to simulate a namespace.

But there seem to be another misunderstanding in your question. Only static data members translate to "global" variables in C. Other "normal" data members are distinct for each instance of your struct.

public static members are global variables, so you can not expect this to work better int C. private static members can be replaced by static variables that are only declared inside your .c file in which you define your functions. The only restriction is then that these are not visible for inline functions, as they would be for inline member functions in C++.

Another thing that should perhaps be added is that you could try to start to think in C when you are doing such a project. Modern C has some tools that don't exist in C++, the intersection of C and C++ is quite a restricted language. Such tools are, e.g:

  • designated initializers that may replace constructors with an empty function body
  • compound literals that provide temporary variables in expressions
  • pointers to VLA (variable length arrays) that can provide you with convenient interfaces for vector and matrix functions
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177