If I have a bunch of C++ code, can I modify it/add to it with C code in any way?
Printable View
If I have a bunch of C++ code, can I modify it/add to it with C code in any way?
Yes, though why do you want to do so? You might be able to just link to the C code as a separate library.
Please show an example of what you're trying to do. It would probably be easier to go the other way, take your C code and add it to a C++ project.
Jim
Then perhaps the correct solution is to learn C++ ;)
C is a lot like C++. As for as variable types, loops, switch, if, do and while statements for example. However C++ allows the programmer more OOP implementation. When I was moving from C to C++ I googled the differences and found a artical that said, a person whom know C++ pretty much knows C but a person who knows C doesn't really know C++. This statement holds a lot of truth and someone can learn the libraries fast in C because they are smaller, C++ is huge but has a bunch of reallz useful tools.
Using pure C in C++ is rather silly in my opinion because there is a huge change that there is a better way to handle it in C++ with a library or perhaps a framework.
Hope that helps.
So what's stopping you? Use a C++ compiler.
C++ allows you to use most of the C functions and libraries with few, if any, changes. But your major problem will probably be interfacing your C mindset with C++.
Jim
Just keep this in mind as you learn other languages and try and solve whatever problem. Think how can you solve this program in C++ not how can I make my C code work in C++. When I was learning Java I tried to get away with just my C and C++ knowledge until someone made that very same comment to me. Here is a small example of something I did not to long ago, it is actually something I learned on here from Lazerlight.
I was using normal C arrays and trying to return the whole array and call it in a different function. Here is an example:
However in C++ you can just do thisCode:
int foo(int myArray[], int counter )
{
//does something with the array here
return myArray[counter++]; //I think I tried something like this
}
I did not compile this or use a compiler to write it so if there are mistakes, sorry. However I think I got my point across.Code:
//if you do not understand the private part yet don't worry its not hard, when you get to classes you will pick it up.
class Example
{
private: std::array<int,3>& myArray;
public:
void setArray()
{
for(int i=0; i<3; i++) //you can put the int inside the loop in C++
{
myArray[i]=i;
// or you can do something even better to make sure you do not go out of bounds
myArray.at(i)=i;
}
}
}
std::array<int,3>& getArray()
{
return myArray;
}
If you're learning C++ from C I'd say it's pretty easy. Just study the basics like what jocdrew is pointing out (object-oriented design).
But more than that, I found the useful things in C++ vs C were constructors and the STL. Pretty much every data structure you've ever thought of is in the STL and even some I would've never guessed.
99% of valid C is valid C++, so you could write C code, compile as C++, and it will work.