-2

I'm C# programmer that wants to learn C++.

On C# I use static classes to use methods without instance a class. Is there something similar to that in C++.

I have found this function and I'm not sure if I can have C functions in a C++ program or all the functions must be class' methods.

vector<string> split(string str, char delimiter) {
  vector<string> internal;
  stringstream ss(str); // Turn the string into a stream.
  string tok;

  while(getline(ss, tok, delimiter)) {
    internal.push_back(tok);
  }

  return internal;
}

Can I have a c source file in a C++ program? Or maybe I need a static class with static methods?

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • C++ is a superset of C, so you can use C-style coding and methods at any time. Did what you tried here work? – tadman Apr 14 '16 at 19:55
  • Yes, you can have classless functions in C++. That does not make it a "C function", however. – zneak Apr 14 '16 at 19:55
  • [You can have functions that are not members of a class.](http://stackoverflow.com/q/4861914/1171191) [You can have `static` functions.](http://www.learncpp.com/cpp-tutorial/812-static-member-functions/) – BoBTFish Apr 14 '16 at 19:56
  • Ok. Thank you very much. Your comments are very helpful. – VansFannel Apr 14 '16 at 19:57
  • @BoBTFish How do you call a source file with functions that are not members of a class? I'm very bad naming things. – VansFannel Apr 14 '16 at 19:59
  • Just a source file, or a source file with free functions if you like. I don't think there is a specific name for it. – Nacho Apr 14 '16 at 20:02
  • @VansFannel How on earth that function is [tag:c] compliant code?? Not any single line of it. – πάντα ῥεῖ Apr 14 '16 at 20:03
  • 1
    I suggest you approach C++ as a completely new language. Put aside your C# knowledge and start from a pure beginner tutorial. – StoryTeller - Unslander Monica Apr 14 '16 at 20:06
  • If you downvote and don't tell why, I'm not going to learn what I've done wrong and I will repeat my 'error'. Thanks. – VansFannel Apr 15 '16 at 07:04

4 Answers4

2

Yes functions do not have to be in a class in C++. If you have multiple source files however you will probably want to supply a prototype for said function in a header.

So in split.hpp

#include <vector>
#include <string>
using namespace std;
vector<string> split(string str, char delimiter);

Then in split.cpp

#include "split.hpp"
vector<string> split(string str, char delimiter) {
  vector<string> internal;
  stringstream ss(str); // Turn the string into a stream.
  string tok;

  while(getline(ss, tok, delimiter)) {
    internal.push_back(tok);
  }

  return internal;
}

Then whenever you need to use the split function you can just include the header using #include "split.hpp".

Lewis GC M
  • 191
  • 3
  • 10
0

You can have a static function:

class Test {
    public:
        Test() {}

        static int GetData() {
            return 1;
        }

        int GetMoreData() const {
            return 2;
        }
};

Test::GetData(); // call this function without an instance of a class

Test A;
A.GetMoreData(); // you can only call this with an instance of a class
ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

You do not need to have a class in C++, you can simply have a function, the following in a cpp file is a valid program:

#include <iostream>
void main()
{
    std::cout << "Hello world" << std::endl;
}

In C# you need to put every method in a class which is why you have static classes.

You can simply call a method not part of a class by name, it is always available. Often they are called global methods for that reason.

You can avoid ambiguity using namespaces but that is a bit more involved than this question.

Guvante
  • 18,775
  • 1
  • 33
  • 64
-1

Yes you can mix 'c' and 'c++' although it's unrecommended. To read more see (https://isocpp.org/wiki/faq/mixing-c-and-cpp)

Dendi Suhubdy
  • 2,877
  • 3
  • 19
  • 20