本文介绍了如何测试有效的变量引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数是否有可能测试其中一个传入变量

(对象的引用)的有效性?

我想要displayString( string& obString)函数验证

obString 1)是否存在2)是一个有效的字符串对象。如果对象不存在,调用任何

方法会导致seg错误。

(代码可以跟随)


# include< string>

#include< iostream>

#include< vector>

using namespace std;


string displayString(string& obString){

cout<< 显示字符串是 << obString<< endl;

return(obString);

}


int main(){


vector< string> obStringList;

string obString;


obString =" test string";

obStringList.push_back(obString);


displayString(obStringList [0]);


displayString(obStringList [1]);


返回(0);

}


localhost cpptest #g ++ -Wall main.cpp

localhost cpptest#。/ a。 out

显示字符串是测试字符串

分段错误

localhost cpptest#

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 #

推荐答案




不,对不起。


john



No, sorry.

john







-

Bob Hairgrove


这篇关于如何测试有效的变量引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 01:44