C语言和c++,java的参数传递对比

C语言参数传递:

C语言中也有引用传递,引用用来减少复制空间的浪费,是身份别名的意思。

值传递

地址传递

引用传递

值传递

不用说了把一个数单纯的放到函数括号里面。

地址传递

两部分,用存地址的变量,接收,值的地址

前者int* a(这种意思,是声明一个存地址的变量),后者&a(这种表示方法获得a的地址),最后再取出所指的地址中的值方法:*a。总之,这三个部分

引用传递:

在声明部分操作,int&a,这样就会是,对接收的变量构造出来一个身份证。在后面使用中,直接用a。引用不能连续的,永远都是一开始的第一个引用对象。(相比于指针,引用的获取值的方式是不一样的,这个是经过了特殊的的加工的获取值的方法)

C++的参数传递

它是和C语言的参数传递使用是一样的

 

Java的参数的参数传递

它特殊在,第一声明的时候,不需要用特殊的符号,&,*,这些统统不用,直接声明,第二用的时候直接用,没有什么特殊的符号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用C++语言实现一个简单的Thrift服务的代码示例: 首先,需要安装Thrift编译器,并且编写一个Thrift定义文件,例如`auth.thrift`: ``` namespace cpp com.example.auth struct User { 1: required string username, 2: required string password } exception AuthException { 1: required string message } service AuthService { string authenticate(1: User user) throws (1: AuthException ex) } ``` 然后,使用Thrift编译器生成C++代码: ``` thrift -r --gen cpp auth.thrift ``` 这会生成一个`gen-cpp`目录,其中包含了自动生成的C++代码。现在,可以使用这些代码实现一个`AuthServiceHandler`类,用于处理客户端的请求,例如: ```c++ #include "gen-cpp/AuthService.h" using namespace std; using namespace com::example::auth; class AuthServiceHandler : virtual public AuthServiceIf { public: string authenticate(const User& user) { // TODO: 实现认证逻辑 return "token"; } }; ``` 在这个示例中,`AuthServiceHandler`类实现了`AuthServiceIf`接口,并重载了`authenticate`方法,用于实现认证逻辑。在实际应用中,可以使用数据库或其他方式来验证用户身份。 最后,可以使用Thrift生成的服务器代码启动一个Thrift服务器,并将`AuthServiceHandler`类作为参数传递给服务器,例如: ```c++ #include <thrift/server/TSimpleServer.h> #include <thrift/transport/TServerSocket.h> #include <thrift/transport/TBufferTransports.h> #include "gen-cpp/AuthService.h" using namespace std; using namespace com::example::auth; using namespace apache::thrift; using namespace apache::thrift::server; using namespace apache::thrift::transport; int main() { int port = 9090; shared_ptr<AuthServiceHandler> handler(new AuthServiceHandler()); shared_ptr<TProcessor> processor(new AuthServiceProcessor(handler)); shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0; } ``` 在这个示例中,使用Thrift生成的`AuthServiceProcessor`类将`AuthServiceHandler`类包装成一个Thrift服务处理器,并将其作为参数传递给`TSimpleServer`服务器。最后,调用`server.serve()`方法启动Thrift服务器。 当Thrift服务器运行后,就可以使用Thrift生成的客户端代码来调用`AuthService`服务的`authenticate`方法了。客户端代码可以使用C++Java、Python等语言来编写,具体实现方式可以参考Thrift官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值