-
Help with clases
Sup
I don't know why classes in c++ are messing me up so much when I understand them in visual basic. I dunno, maybe its just the syntax but I have got to get this down so I can move on to windows programming lol.
I have 2 or 3 books that deal some with classes in c++ but all of them do not answer my 2 questions.
If you could please answer them, I would be thankful =)
1. What is the proper way to passs strings to a class and also to return them?
2. What would be the proper way to pass an array of strings and also to return them.
I would guess on number 2 that you may have to send each variable in the array one by one but would like to confirm =)
Thank You for any help
-
#2.
you can have your funciton accept the array as a reference variable...
ex. void myArrayFunc(int &myArray[100]);
so now myArray will be whatever it is last assigned to :) that should be easier than calling 1 function numerous times :)
-
1: The easiest way (especially for a VB programmer) is to use the string object:
Code:
#include <string>
using std::string;
string func()
{
return string("Hello");
}
int main()
{
string x = func();
cout << x.c_str() << endl;
return 0;
}
2: You can create an array of string objects and pass them as Steve said, or you can use a vector:
Code:
#include <string>
#include <vector>
using std::string;
using std::vector;
void func(vector<string>& strings)
{
for(int i = 0; i < strings.size(); ++i)
cout << strings[i].c_str() << endl;
}
int main()
{
vector<string> x;
x.push_back(string("Hello"));
x.push_back(string("Goodbye"));
func(x);
return 0;
}
Z.