假设我有一个.hpp文件,其中包含一个带有公共静态方法和私有静态成员/变量的简单类.这是一个示例类:
class MyClass
{
public:
static int DoSomethingWithTheVar()
{
TheVar = 10;
return TheVar;
}
private:
static int TheVar;
}
Run Code Online (Sandbox Code Playgroud)
当我打电话给:
int Result = MyClass::DoSomethingWithTheVar();
Run Code Online (Sandbox Code Playgroud)
我希望"结果"等于10;
相反,我得到(在第10行):
undefined reference to `MyClass::TheVar'
Run Code Online (Sandbox Code Playgroud)
第10行是"TheVar = 10;" 从方法.
我的问题是它是否可以从静态方法(DoSomethingWithTheVar)访问私有静态成员(TheVar)?
所以假设我有一个main.pl
脚本,在那个脚本中我需要声明一些变量(任何类型的变量常量或正常),并且这些变量需要通过我将从该main.pl
脚本自动包含的所有脚本和模块提供.
我的意思,如果我有一个变量$myVar
中main.pl
,并从main.pl
我需要script1.pl
,script2.pl
或script3.pm
和那些我需要访问脚本的人$myVar
,你会访问特定的脚本或模块定义的任何变种.
我在网上搜索过,但我只找到了一些例子,你可以从你包含的脚本中访问变量或从模块中提取变量; 但这不是我想要的.
是不是像PHP中的关键字,您将使用global $var1, $var2
等从父脚本使用变量?
任何示例,文档或文章都是可以接受的 - 基本上可以帮助我完成任何事情的任何内容都会有所帮
我遇到过这个代码示例,我记得我之前见过它,但我不知道它是什么以及它做了什么?我在互联网上搜索但没有运气.
码:
class C
{
int x; // a non-static variable, implicitly private
public:
C() : x(0) {} // default constructor
// a static member function, which uses a non-static variable perfectly well
static int Incr(C& instance) { return ++(instance.x); }
} g_c;
int main(void)
{
C c2;
return C::Incr(g_c) + C::Incr(c2);
}
Run Code Online (Sandbox Code Playgroud)
是什么g_c
在最后一堂课托架装置后?
假设我有一个SomeManager
用于跟踪另一个类的实例的主类SomeClass
.在SomeClass
构造时,它调用一个SomeManager
将指针传递给它的方法.然后SomeManager
获取该指针并将其推入向量.SomeClass
调用析构函数的另一个函数是SomeManager
从向量中删除它的指针.
所以,我的问题是.当一个实例SomeClass
通过move运算符或构造函数移动时.这是地址更改,我必须删除旧地址并添加新地址吗?
我从我读过的内容中得到了一些想法,但我不确定,我不想搞砸.
我是C++的"隐藏/黑暗的地方"的新手,我想知道如何从我正在运行当前可执行文件的目录中的不同目录或子目录中加载.dll文件
例如:
./MyAppDirectory
/MyApp.exe
/SomeDLL.dll
/AnotherDLL.dll
/SubDirectory
/SomeDLL2.dll
/AnotherDLL2.dll
/YetAnotherDLL.dll
/...
Run Code Online (Sandbox Code Playgroud)
因此,"MyApp.exe"会自动从其根文件夹"MyAppDirectory"加载"SomeDLL.dll"和"AnotherDLL.dll",但我还希望能够加载"SomeDLL2.dll","AnotherDLL2.dll","YetAnotherDLL".从"MyAppDirectory"文件夹中的"SubDirectory"文件夹中输入"等".
我一直在做一些搜索,从我发现的唯一解决方案是:
但是他们都有一些不好的方面(这里不值得一提)而且这不是我真正需要的.另一个解决方案是通过"特定于应用程序的路径!" 涉及使用Windows注册表,似乎比之前提到的略好.
但是,我需要能够使用C++在"MyApp.exe"中执行此操作,而无需使用外部方法.
我使用的是MinGW 4.7.2,我的IDE是Code :: Blocks 12.11,我的操作系统是Windows XP SP3 Pro x86.
任何参考,教程,文档,示例等都被接受,谢谢你的时间:D
我正在尝试使用 Zoho 的 SMTP 服务器从 MyBB 安装发送注册电子邮件。但是,电子邮件永远不会发送,当我检查日志时,我注意到错误是。
Month Day Year:Hour:Minute host=smtp.zoho.com tls=on auth=on user=user[at]mydomain.com from=user[at]mydomain.com recipients=some.user[at]gmail.com smtpstatus=553 smtpmsg='553 Relaying disallowed' errormsg='the server did not accept the mail' exitcode=EX_UNAVAILABLE
Run Code Online (Sandbox Code Playgroud)
(部分部分被屏蔽以隐藏私人信息!)
这是php.ini中设置的sendmail路径
sendmail_path = "/usr/bin/msmtp -C /etc/msmtp/myserver --logfile /var/log/msmtp/myserver.log -a default -t"
Run Code Online (Sandbox Code Playgroud)
(部分部分被屏蔽以隐藏私人信息!)
这是 MSMTP 的 myserver 配置
# Define here some setting that can be useful for every account
defaults
logfile /var/log/msmtp/general.log
# Settings for default account
account default
protocol smtp
host smtp.zoho.com
tls on
tls_starttls off …
Run Code Online (Sandbox Code Playgroud) 假设我有一个主 DLL,其中有一个这样的类:
class Test
{
public:
typedef std::unordered_map< std::type_index, int > Map;
template < typename T > void SetValue(int val)
{
SetValue(std::type_index(typeid(T)), val);
}
template < typename T > int GetValue()
{
return GetValue(std::type_index(typeid(T)));
}
protected:
// Defined in .cpp file
void SetValue(const std::type_index & idx, int val)
{
m_Map[idx] = val;
}
// Defined in .cpp file
int GetValue(const std::type_index & idx)
{
Map::const_iterator itr = m_Map.find(idx);
if (itr != m_Map.cend())
{
return itr->second;
}
return 0;
}
private: …
Run Code Online (Sandbox Code Playgroud) 我已经注意到PHP中单例的奇怪行为,没有更好的方法来解释这个但是举个例子.
假设我有以下单例类:
class Singleton
{
protected function __construct()
{
// Deny direct instantion!
}
protected function __clone()
{
// Deny cloning!
}
public static function &Instance()
{
static $Instance;
echo 'Class Echo'.PHP_EOL;
var_dump($Instance);
if (!isset($Instance)) {
$Instance = new self;
}
return $Instance;
}
}
Run Code Online (Sandbox Code Playgroud)
并具有以下功能:
function Test($Init = FALSE)
{
static $Instance;
if ($Init === TRUE && !isset($Instance)) {
$Instance =& Singleton::Instance();
}
echo 'Function Echo'.PHP_EOL;
var_dump($Instance);
return $Instance;
}
Run Code Online (Sandbox Code Playgroud)
当我使用以下内容时:
Test(TRUE);
Test();
Singleton::Instance();
Run Code Online (Sandbox Code Playgroud)
输出是:
Class Echo
NULL
Function …
Run Code Online (Sandbox Code Playgroud) 说我有这个C函数:
__declspec(dllexport) const char* GetStr()
{
static char buff[32]
// Fill the buffer with some string here
return buff;
}
Run Code Online (Sandbox Code Playgroud)
而这个简单的Lua模块:
local mymodule = {}
local ffi = require("ffi")
ffi.cdef[[
const char* GetStr();
]]
function mymodule.get_str()
return ffi.C.GetStr()
end
return mymodule
Run Code Online (Sandbox Code Playgroud)
如何从C函数中获取返回的字符串作为Lua字符串:
local mymodule = require "mymodule"
print(mymodule.get_str())
Run Code Online (Sandbox Code Playgroud) 我分配了大缓冲区,然后拆分成多个大小的块.这些尺寸从32开始,然后在每次增加时乘以2.
struct Node
{
Node* Next;
void* Data;
const unsigned Size;
};
Node* m_Buffers[16];
Run Code Online (Sandbox Code Playgroud)
这意味着缓冲区m_Buffers[0]
的大小为32,缓冲区m_Buffers[1]
的大小为64,依此类推.
并且一个函数接受一个数字并返回一个指定数字可以舍入到的大小的缓冲区.
void * GetBuffer(unsigned size)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我请求一个384的缓冲区,那么我需要能够在512处将其舍入并从中返回缓冲区m_Buffers[4]
.
到目前为止,我正在使用循环来舍入:
void * GetBuffer(unsigned size)
{
unsigned buffer_size = 32;
while (buffer_size < size)
{
buffer_size *= 2;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
但我很好奇是否有一种更好的方法可以不涉及循环.如果有一种方法可以将舍入数字转换为数组中的索引而不使用switch语句.
说实话,我甚至不确定标题是否正确.所以我为此道歉.