相关疑难解决方法(0)

C/C++性能全局与获取/设置方法

我看到这个问题询问全局变量是否坏.

当我想到它的后果时,我能想出的唯一论据是,在某些情况下它们是必要的可能是出于性能原因.

但是,我对此并不十分肯定.所以我的问题是,使用全局比使用get/set方法调用更快吗?

c c++ methods performance global

3
推荐指数
1
解决办法
1991
查看次数

当我们可以选择将变量设为外部时,为什么我们通过引用传递?

假设我们有一个数组说:

int arr[1000];
Run Code Online (Sandbox Code Playgroud)

我有一个适用于该数组的函数说:

void Func(void);
Run Code Online (Sandbox Code Playgroud)

当我可以将arr [1000]作为main()外部的外部变量时,为什么需要通过引用传递(通过更改void)?

  1. 有什么区别?有什么区别吗?
  2. 为什么人们更喜欢通过引用传递而不是将其作为外部传递?(我自己认为将它制作成外部更容易).

c pointers pass-by-reference

3
推荐指数
2
解决办法
2253
查看次数

在C中传递和返回变量

我正在尝试将变量从一个函数传递到另一个函数.

例如:

FuncA:从用户接收3个输入,我想在FuncB中使用这3个输入.

我该怎么办?我只是从FuncA返回3个值并将其作为Func B的参数传递给我吗?

我会这样做吗?**不使用指针.

int FuncA(void);
int FuncB(int A, int B, int C, int D, int E);

int main(void)
{
    FuncA(void);
    FuncB(A,B,C);
}

int FuncA(void)
{
    printf("Enter 3 number:");
    scanf("%d %d %d" &A, &B, &C);
    return A, B, C;
}

int FuncB(int A, int B, int C)
{
    .............
}
Run Code Online (Sandbox Code Playgroud)

c parameter-passing

2
推荐指数
2
解决办法
4万
查看次数

PHP:MVC 用户身份验证和全局用户对象?

我正在用 PHP 编写我的第一个真正的 MVC 应用程序。我没有使用框架,因为我的应用程序太小了,我认为从头开始编写所有内容会更快。

在我的应用程序中,用户可以注册、登录然后编写/编辑/删除内容。每条内容都通过数据库中的用户 ID 列引用其所有者。

我现在即将实施用户访问限制(从某种意义上说,用户只能查看/编辑/删除他们自己的内容/项目/模型)。我想知道应该在哪里检查“有效访问”以及在哪里实例化用户对象。

我的意思是,我绝对需要有关控制器、模型和视图中当前用户的信息。因此,我在考虑是否可以拥有一个全局用户对象(在 index.php 中定义)来存储所有用户信息,以便我可以从应用程序的每个部分轻松访问它。

目前,此代码段授予我的控制器访问用户信息的权限,然后我还将这些信息存储在传递给视图的数据数组中:

class Controller {
    protected $data, $id, $user;
    public function __construct($action = null, $data = null) {
        if (User::isLoggedIn()) {
            $this->user = new User($_SESSION['user']);
            $this->data['user'] = $this->user;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

按照这种模式,我必须将用户信息传递给我创建的每个模型,或者让它们实例化自己的用户对象。

去这里的路是什么?全局用户对象,在每个模型中实例化还是将用户对象作为参数传递给模型和视图?

感谢您的帮助!

php authentication model-view-controller

2
推荐指数
1
解决办法
6767
查看次数

标准库中外部全局变量的用法

  1. 全局变量通常不被鼓励。C++ 标准库中具有外部链接的全局变量背后的原理是什么?

  2. extern变量真的只是声明而不是定义吗?

std::call_once一个例子是声明具有mutex.h 外部链接的线程局部全局变量的 gcc 实现:

  extern __thread void* __once_callable;
  extern __thread void (*__once_call)();
Run Code Online (Sandbox Code Playgroud)

  /// @cond undocumented
# ifdef _GLIBCXX_HAVE_TLS
  // If TLS is available use thread-local state for the type-erased callable
  // that is being run by std::call_once in the current thread.
  extern __thread void* __once_callable;
  extern __thread void (*__once_call)();

  // RAII type to set up state for pthread_once call.
  struct once_flag::_Prepare_execution
  {
    template<typename _Callable>
      explicit
      _Prepare_execution(_Callable& __c)
      {
    // Store address in thread-local pointer: …
Run Code Online (Sandbox Code Playgroud)

c++ global-variables c++11

2
推荐指数
1
解决办法
334
查看次数

这些变量的范围是什么?

请参阅下面的C++代码片段:

#include .....

Class1 class1;
Class2 class2;
...

void Class3::foo() {
    ...
}
Run Code Online (Sandbox Code Playgroud)

变量有哪些:class1和class2?它们是全局变量吗?静态变量?实际上这些是什么?在C++ OO编程中,使用这些是不错的做法,因为文件中任何类的任何成员函数都可以访问它们?

对不起初学者的问题.

谢谢.

c++

1
推荐指数
1
解决办法
163
查看次数

Ruby中的类变量

我在本教程中遇到了以下示例:

class Song
  @@plays = 0

  def initialize(name, artist, duration)
    @name = name
    @artist = artist
    @duration = duration
    @plays = 0
  end

  def play
    @plays += 1
    @@plays += 1
    "This song: #@plays plays. Total #@@plays plays."
  end
end

s1 = Song.new("Song1", "Artist1", 234)    # test songs
s2 = Song.new("Song2", "Artist2", 345)   

puts s1.play
puts s2.play
puts s1.play
puts s1.play 
Run Code Online (Sandbox Code Playgroud)

@@只能在Song课程中礼貌地播放吗?这篇评论提出了不建议使用类变量的观点.是不是b/c它们在日常使用中通常不需要,并且在使用时会产生很多调试问题?

ruby class-variables

1
推荐指数
1
解决办法
2539
查看次数

具有2个功能的简单程序不起作用

我是C++的新手并尝试制作两个简单的函数,但出了点问题.

我正在尝试执行以下操作:

1.Function for input some data.
2.Function to show what data is input.
Run Code Online (Sandbox Code Playgroud)

我只是想简单一点.我到目前为止写的代码是:

#include <iostream>
void masiv()
{
  int x[10];
  int n, i;
  int min;
  int max=0, imax=0, imin;

  cout << "Enter the number of elements: ";
  cin >> n;

  for(i=0; i < n; i++)
  {
      cout << "Input value for x["<<i<<"]=";
      cin >> x[i];

  if (min > x[i])
  {
      min = x [i];
      imin = i;
  }

  if (max < x[i])
  {
     max = x[i];
     imax = …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
229
查看次数

使用g ++ .cpp文件编译gcc .o文件

我有两个文件,print_permu.cgen_permu.cpp.我希望将print_permu.c文件编译成目标文件,然后使用目标文件进行编译gen_permu.cpp,其中包含对函数的调用print_permu.c.

print_permu.c

#include<stdlib.h>
#include<stdio.h>

typedef char byte;

char *printable=NULL;
size_t pos,size,*char_cnt;

void __print_permu_recurse()
{
        if( pos==size )
        {
                printf("%s\n",printable);
                return;
        }
        byte iter = 25;
        while( iter>=0 )
        {
                if( char_cnt[iter] )
                {
                        printable[pos] = 'a'+iter;
                        --char_cnt[iter];
                        ++pos;
                        __print_permu_recurse();
                        --pos;
                        ++char_cnt[iter];
                }
                --iter;
        }
}

void print_permu(size_t *char_count)
{
        char_cnt = char_count;
        for(pos = 0,size = 0 ; pos<26 ; ++pos)
                size += char_count[pos];

        printable = (char*)malloc(sizeof(char)*(size+1));
        printable[size] = '\0'; …
Run Code Online (Sandbox Code Playgroud)

c c++ gcc g++ object-files

1
推荐指数
1
解决办法
774
查看次数

Qt C++创建一个可供所有类访问的全局变量

是否有可能为我的程序中的所有类创建一个全局可访问的变量,如果我从一个类更改它的值,它也会为所有其他类更改?

如果是这样,我该如何实现?

c++ qt global-variables

1
推荐指数
1
解决办法
3596
查看次数