J
joenuts
Is it possible for a function to test one of it's passed in variables
(reference to object) for validity?
I would like the displayString( string &obString) function to verify
that obString 1) exists 2) is a valid string object. calling any
methods of said object cause a seg fault if the object doesnt exist.
(code to follow)
#include <string>
#include <iostream>
#include <vector>
using namespace std;
string displayString( string &obString) {
cout << "The display string is " << obString << endl;
return( obString);
}
int main() {
vector<string> obStringList;
string obString;
obString = "test string";
obStringList.push_back( obString);
displayString( obStringList[0]);
displayString( obStringList[1]);
return( 0);
}
localhost cpptest # g++ -Wall main.cpp
localhost cpptest # ./a.out
The display string is test string
Segmentation fault
localhost cpptest #
(reference to object) for validity?
I would like the displayString( string &obString) function to verify
that obString 1) exists 2) is a valid string object. calling any
methods of said object cause a seg fault if the object doesnt exist.
(code to follow)
#include <string>
#include <iostream>
#include <vector>
using namespace std;
string displayString( string &obString) {
cout << "The display string is " << obString << endl;
return( obString);
}
int main() {
vector<string> obStringList;
string obString;
obString = "test string";
obStringList.push_back( obString);
displayString( obStringList[0]);
displayString( obStringList[1]);
return( 0);
}
localhost cpptest # g++ -Wall main.cpp
localhost cpptest # ./a.out
The display string is test string
Segmentation fault
localhost cpptest #