C ++:在另一个函数里声明一个函数的用法是什么? [英] C++: what's the usage of declaring a function inside another function?

查看:200
本文介绍了C ++:在另一个函数里声明一个函数的用法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stanley Lippman的C ++引擎在第234页提到,


通常,在本地声明一个函数是个坏主意。但是,
解释了范围如何与重载交互,我们将违反这个
练习并使用局部函数声明。




  ... 
void print(const string&);
void print(double); //重载打印函数
void fooBar(int ival)
{...
//错误的做法:通常在局部范围声明函数是一个坏主意
void print (int); // new scope:hide previous instances of print
print(ival); // ok:print(int)is visible
print(3.14); // ok:calls print(int); print(double)is hidden
}

然而,所以,在函数体内声明一个函数?



我记得Scheme中类似的代码:

 (define(PowerSet set)
(if(null?set)'(())
(let((PowerSetAfterHead(PowerSet(cdr set))))
(append PowerSetAfterHead
(PowerSet(cdr set))
(map
(lambda(subset)
(cons(car set)subset


(PowerSet(cdr set))





/ pre>

它通常用于隐藏一个局部函数的滥用,它只打算用于外层函数的使用?像内部类?

解决方案

当调用函数时存在歧义时,会出现问题。因此,为了避免某些范围中的模糊性,您可以重新声明所需的函数,隐藏所有其他函数具有相同的外部范围名称。



考虑下面的简单示例。如果您运行以下程序

  #include< iostream& 

void f(int){std :: cout<< f(int)< std :: endl; }
void f(double){std :: cout< f(double)< std :: endl; }

int main()
{
f(10l);

return 0;
}

您将收到类似

的错误

prog.cpp:10:9:错误:调用重载的'f(long int)'是不明确的f(10l);



将在main中添加一个函数f的声明。

  #include< iostream> 

void f(int){std :: cout<< f(int)< std :: endl; }
void f(double){std :: cout< f(double)< std :: endl; }

int main()
{
void f(double);
f(10l);

return 0;
}

代码将被编译,输出将类似于

  f(double)


Stanley Lippman's "C++ primer" mentioned, in page 234 that

Ordinarily, it is a bad idea to declare a function locally. However, to explain how scope interacts with overloading, we will violate this practice and use local function declarations.

...
void print(const string &);
void print(double); // overloads the print function
void fooBar(int ival)
{ ...
  // bad practice: usually it's a bad idea to declare functions at local scope
  void print(int); // new scope: hides previous instances of print
  print(ival); // ok: print(int) is visible
  print(3.14); // ok: calls print(int); print(double) is hidden
}

Nevertheless, which situation it might make sense to do so, declaring a function inside the body of a function?

I recall similar code in Scheme:

(define (PowerSet set)
   (if (null? set) '(())
      (let ((PowerSetAfterHead (PowerSet (cdr set) ) ))
         (append PowerSetAfterHead
            (PowerSet (cdr set) )
            (map
               (lambda (subset)
                  (cons (car set) subset
                  )
               )
               (PowerSet (cdr set) )                                 
            )
         )
      )
   )
)

Is it normally used to hide from misuse of a "local function" that intends for the outer function's usage only? something like internal class? Or is it useful in some design patterns?

解决方案

The problem arises when there is an ambiguity in calling a function. Thus to avoid the ambiguity in some scope you can redeclare the required function that hides all other functions with the same name of the outer scope.

Consider the following simple example. If you run the following program

#include <iostream>

void f( int ) { std::cout << "f( int )" << std::endl; }
void f( double ) { std::cout << "f( double )" << std::endl; }

int main() 
{
    f( 10l );

    return 0;
}

you will get an error like

prog.cpp:10:9: error: call of overloaded 'f(long int)' is ambiguous f( 10l );

But if you will add a declaration of function f within main like this

#include <iostream>

void f( int ) { std::cout << "f( int )" << std::endl; }
void f( double ) { std::cout << "f( double )" << std::endl; }

int main() 
{
    void f( double );
    f( 10l );

    return 0;
}

the code will be compiled and the output will look like

f( double )

这篇关于C ++:在另一个函数里声明一个函数的用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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