如何测试有效的变量引用? [英] How to test for valid variable reference ?

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

问题描述

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

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

我想要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 #

推荐答案

joenuts写道:
joenuts wrote:
函数是否可以测试其中一个传入变量
(引用对象)的有效性?
我想使用displayString(string& obString)函数来验证obString 1)是否存在2)是一个有效的字符串对象。如果对象不存在则调用所述对象的任何
方法会导致seg错误。
(代码可以跟随)
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)




不,对不起。


john



No, sorry.

john


joenuts写道:
joenuts wrote:
函数是否可以测试其中一个'传入变量
(引用对象)是否有效?
我想使用displayString(string& obString)函数来验证obString 1)是否存在2)是一个有效的字符串宾语。如果对象不存在则调用所述对象的任何
方法会导致seg错误。
(代码可以遵循)

#include< string>
#include< ; iostream>
#include< vector>
使用命名空间std;

字符串displayString(string& obString){
cout<< 显示字符串是 << obString<< endl;
返回(obString);
}
int main(){

vector< string> obStringList;
字符串obString;

obString =" test string" ;;
obStringList.push_back(obString);

displayString(obStringList [0] );

displayString(obStringList [1]);


使用obStringList.at(1)代替检查。抛出

out_of_range

返回(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]);
use obStringList.at( 1 ) instead, as it does bounds checking. It throws
out_of_range

return( 0);
}

localhost cpptest # g++ -Wall main.cpp
localhost cpptest # ./a.out
The display string is test string
Segmentation fault
localhost cpptest #



2005年11月21日14:28:54 -0800,joenuts <乔***** @ gmail.com>写道:
On 21 Nov 2005 14:28:54 -0800, "joenuts" <jo*****@gmail.com> wrote:
函数是否有可能测试其中一个传递的变量
(引用对象)的有效性?
我想displayString(string& obString)函数验证obString 1)是否存在2)是一个有效的字符串对象。如果对象不存在,则调用所述对象的任何
方法会导致seg错误。
(代码遵循)


引用自动保证(1)和(2)是真的

INSIDE函数体,除非你做了什么使

引用的原始对象变得无效。你的段错误来自

试图访问你的向量的第二个元素,它只有一个

元素。程序永远不会第二次进入函数




如果你使用vector :: at()而不是vector :: operator [] ,你会得到一个

C ++异常(std :: out_of_range)。进一步索引到一个向量而不是

有运算符[]的元素导致未定义的行为(即

崩溃)。

#include< string> ;
#include< iostream>
#include< vector>
使用命名空间std;

字符串displayString(string& obString){
cout<< 显示字符串是 << obString<< endl;
返回(obString);
}
int main(){

vector< string> obStringList;
字符串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)
References automatically guarantee that both (1) and (2) are true
INSIDE the function body, unless you have done something to make the
original object referenced become invalid. Your segfault comes from
trying to access the second element of your vector which has only one
element. The program never makes it to the function the second time
around.

If you use vector::at() instead of vector::operator[], you will get a
C++ exception (std::out_of_range). Indexing further into a vector than
there are elements with operator[] causes undefined behavior (i.e.
crashes).
#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 #




-

Bob Hairgrove
否***** *****@Home.com


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆