我有一个Visual Studio 2008 C++应用程序,其中基类A_Base
需要实例化一个类型由父类定义的数据成员.例如:
template< typename T >
class A_Base
{
public:
typedef typename T::Foo Bar; // line 10
private:
Bar bar_;
};
class A : public A_Base< A >
{
public:
typedef int Foo;
};
int _tmain( int argc, _TCHAR* argv[] )
{
A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,似乎编译器不知道是什么T::Foo
,直到它为时已晚,我得到这样的错误:
1>MyApp.cpp(10) : error C2039: 'Foo' : is not a member of 'A'
1> MyApp.cpp(13) : see declaration of 'A'
1> MyApp.cpp(14) : see reference to class …
Run Code Online (Sandbox Code Playgroud) 我有一个Visual Studio 2008 C#.NET 3.5应用程序P/Invokes一个接受文件句柄作为参数的本机方法.最初,我只是使用FileStream.SafeFileHandle.DangerousGetHandle()来获取文件句柄.但是,在启用FX COP之后,我收到了CA2001的警告.因此,经过一番研究后,我发现了"约束执行区域".这对我来说是新的,我还没有看到很多关于它的信息.我希望有经验的人可以看看并验证我是否已正确完成此操作.
class MyClass
{
public static bool Write(string filename)
{
using (var fs = new System.IO.FileStream(filename,
System.IO.FileMode.Create,
System.IO.FileAccess.Write,
System.IO.FileShare.None))
{
bool got_handle;
bool result;
System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally
{
fs.SafeFileHandle.DangerousAddRef(ref got_handle);
result = NativeMethods.Foo(fs.SafeFileHandle.DangerousGetHandle());
if (got_handle)
fs.SafeFileHandle.DangerousRelease();
}
return result;
}
}
}
internal sealed class NativeMethods
{
[DllImport("mylib.dll",
EntryPoint = "Foo",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Unicode,
ExactSpelling = true,
SetLastError = true)]
public static extern bool Foo(IntPtr hFile);
}
Run Code Online (Sandbox Code Playgroud)
谢谢,PaulH
我有一个类的功能,我想依赖于一组插件策略.但是,我不确定如何从一个任意数量的类派生一个类.
下面的代码是我想要实现的一个例子.
// insert clever boost or template trickery here
template< class ListOfPolicies >
class CMyClass : public ListOfPolicies
{
public:
CMyClass()
{
// identifiers should be the result of OR-ing all
// of the MY_IDENTIFIERS in the TypeList.
DWORD identifiers;
DoSomeInitialization( ..., identifiers, ... );
}
int MyFunction()
{
return 100;
}
// ...
};
template< class T >
class PolicyA
{
public:
enum { MY_IDENTIFIER = 0x00000001 };
int DoSomethingA()
{
T* pT = static_cast< T* >( this …
Run Code Online (Sandbox Code Playgroud) 我有一个C++类层次结构,看起来像这样:
class A;
class B
{
public:
B( A& a ) : a_( a )
{
};
private:
A& a_;
};
class MyObject
{
public:
MyObject() : b_( a_ )
{
};
private:
A a_;
B b_;
};
Run Code Online (Sandbox Code Playgroud)
偶尔会发生在B的析构函数中,我将获得与其A的引用相关的无效访问异常.看来A在B之前被销毁.
使用类成员初始化其他成员是否存在固有的错误?是否无法保证破坏的顺序?
谢谢,PaulH
我正在测试一个C++类,其中包含许多基本相同形式的函数:
ClassUnderTest t;
DATATYPE data = { 0 };
try
{
t.SomeFunction( &data );
}
catch( const SomeException& e )
{
// log known error
}
catch( ... )
{
// log unknown error
}
Run Code Online (Sandbox Code Playgroud)
由于有很多这些,我以为我会写一个功能来完成大部分繁重的工作:
template< typename DATA, typename TestFunction >
int DoTest( TestFunction test_fcn )
{
DATA data = { 0 };
try
{
test_fcn( &data );
}
catch( const SomeException& e )
{
// log known error
return FAIL;
}
catch( ... )
{
// log unknown …
Run Code Online (Sandbox Code Playgroud) 我有一个Visual Studio 2008 C++应用程序,它执行以下操作:
template< typename Fcn >
inline void Bar( Fcn fcn ) // line 84
{
fcn();
};
template< typename Fcn >
inline void Foo( Fcn fcn )
{
// this works fine
Bar( fcn );
// this fails to compile
boost::bind( Bar, fcn )();
};
int main()
{
SYSTEM_POWER_STATUS_EX status = { 0 };
Foo( boost::bind( ::GetSystemPowerStatusEx, &status, true ) ); // line 160
return 0;
}
Run Code Online (Sandbox Code Playgroud)
*对GetSystemPowerStatusEx()的调用仅用于演示.在那里插入您最喜欢的电话,行为是一样的.
当我编译这个时,我得到84个错误.除非被问到,否则我不会发布所有内容,但他们从这开始:
1>.\MyApp.cpp(99) : error C2896: 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> boost::bind(M T::* …
Run Code Online (Sandbox Code Playgroud) 我在运行Windows XP SP3和Apache 2.2.21的Web服务器上使用PHP 5.3.8,我需要创建一个互斥锁.经过一些研究,我遇到了flock命令并实现了如下:
class Mutex
{
private $lock_ = null;
// create a mutex with with a given id. This ID must be system unique.
// [string] id - a unique id
// return - true on success
public function Initialize($id)
{
$this->lock_ = fopen($id, 'a');
return is_resource($this->lock_);
}
// destroy the mutex
public function Destroy()
{
$result = false;
if (is_resource($this->lock_));
{
$result = flock($this->lock_, LOCK_UN);
$result &= fclose($this->lock_);
$this->lock_ = null;
}
return $result; …
Run Code Online (Sandbox Code Playgroud) 我在Windows 8.1 x64上使用C#.NET 4.0应用程序需要很长时间(18秒)才能执行某些操作.我想尝试诊断为什么会发生这种情况.Process Monitor跟踪显示:
??????????????????????????????????????????????????????????
? Relative Time ? Operation ?
??????????????????????????????????????????????????????????
? 00:02.000000 ? Thread Create ?
? 00:02.000100 ? Thread Create ?
? 00:04.000000 ? Thread Create ?
? 00:04.000100 ? Thread Create ?
? 00:04.000200 ? Thread Create ?
? 00:04.000300 ? Thread Exit ?
? 00:04.000400 ? Thread Create ?
? 00:04.000500 ? TCP Disconnect ?
? 00:04.000600 ? Thread Exit ?
? 00:06.000000 ? Thread Create ?
? 00:06.000100 ? Thread Create ?
? …
Run Code Online (Sandbox Code Playgroud) 我有一个Visual Studio 2008 C++应用程序,它有几种类型的对象,它们来自一个共同的基础.例如:
class Base
{
public:
std::string Time() const { /*return formatted time*/; };
private:
SYSTEMTIME time_;
};
class Foo : public Base
{
public:
const char* FooFunc() const { return "Hello from foo!"; };
};
typedef std::vector< Base > BaseList;
class Bar : public Base
{
public:
const char* BarFunc() const { return "Hello from bar!"; };
void push_back( const Base& obj ) { list_.push_back( obj ); };
BaseList::const_iterator begin() const { return list_.begin(); };
BaseList::const_iterator end() …
Run Code Online (Sandbox Code Playgroud) 我有一个Visual Studio 2008 C#.NET 3.5应用程序,其中我有一个字符串,其中包含由分号分隔的数字列表.
string num_list = "1;2;3;4;201;2099;84"
Run Code Online (Sandbox Code Playgroud)
我想将其转换为List<int>
.有比这更简单的方法吗?
List<int> foo = new List<int>();
foreach (string num in num_list.Split(';'))
foo.Add(Convert.ToInt32(num));
Run Code Online (Sandbox Code Playgroud)
谢谢,PaulH