我处于一种情况,我需要在另一个类的实例中实例化一个带有参数的类.这是原型:
//test.php
class test
{
function __construct($a, $b, $c)
{
echo $a . '<br />';
echo $b . '<br />';
echo $c . '<br />';
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要使用下面的类的cls函数来实例化上面的类:
class myclass
{
function cls($file_name, $args = array())
{
include $file_name . ".php";
if (isset($args))
{
// this is where the problem might be, i need to pass as many arguments as test class has.
$class_instance = new $file_name($args);
}
else
{
$class_instance = new $file_name();
}
return $class_instance;
} …Run Code Online (Sandbox Code Playgroud) 希望有人可以帮助我解决Viewpagers和保存数据方面的轻微问题/困惑.
问题:
滚动浏览四个视图时,第一个视图有两个微调器,两个显示字符串或选定项目的文本视图.如果我滚动到第三页并返回第二页,则第一个视图中的数据将丢失.hense需要保存数据.
这将在下面列出的两个例程中完成吗?(最好的猜测是)如果是这样,需要说明什么样的命令?
码:
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
Run Code Online (Sandbox Code Playgroud)
EXTRA INFO:正在使用viewpager
public Object instantiateItem(View collection, int position) {
}
Run Code Online (Sandbox Code Playgroud)
完整的方法列表如下:
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public void restoreState(Parcelable …Run Code Online (Sandbox Code Playgroud) 在学习Python的数据模型时,我正在使用该__new__方法从现有对象创建对象.以下是一些创建各种类型的新对象的示例:
x = 2; print type(x).__new__(x.__class__)
x = {}; print type(x).__new__(x.__class__)
x = [1,2]; print type(x).__new__(x.__class__)
x = 2.34; print type(x).__new__(x.__class__)
x = '13'; print type(x).__new__(x.__class__)
x = 1.0j; print type(x).__new__(x.__class__)
x = True; print type(x).__new__(x.__class__)
x = (1,2); print type(x).__new__(x.__class__)
Run Code Online (Sandbox Code Playgroud)
但是,以下三个实验给出了错误:
x = None; print type(x).__new__(x.__class__)
x = lambda z: z**2; print type(x).__new__(x.__class__)
x = object; print type(x).__new__(x.__class__)
Run Code Online (Sandbox Code Playgroud)
错误是(分别):
TypeError: object.__new__(NoneType) is not safe, use NoneType.__new__()
TypeError: Required argument 'code' (pos 1) not found
TypeError: type() takes …Run Code Online (Sandbox Code Playgroud) g ++编译器有一个生成宏扩展代码(-E)的标志,所以我想知道在实际编译发生之前是否有一种方法可以在模板实例化之后查看程序.
假设我有以下元函数:
template <typename T>
struct make_pair {
using type = std::pair<
typename std::remove_reference<T>::type,
typename std::remove_reference<T>::type
>;
};
Run Code Online (Sandbox Code Playgroud)
相反,它会提高编译速度(或其他)吗?
template <typename T>
struct make_pair {
using without_reference = typename std::remove_reference<T>::type;
using type = std::pair<without_reference, without_reference>;
};
Run Code Online (Sandbox Code Playgroud)
我看到两种可能性:
编译器每次看到它都必须做一些工作typename std::remove_reference<T>::type.使用中间别名具有某种"缓存"行为,这允许编译器只执行一次工作.
编译时性能是根据编译器必须执行的模板实例化的数量来衡量的.因为std::remove_reference<T>::type引用的类型相同std::remove_reference<T>::type,所以在这两种情况下只需要一个模板实例化,因此两个实现都是等效的WRT编译时性能.
我认为B是对的,但我想确定一下.如果答案结果是编译器特定的,我最感兴趣的是知道Clang和GCC的答案.
编辑:
我对测试程序的编译进行了基准测试,以便使用一些数据.测试程序做了类似的事情:
template <typename ...> struct result;
template <typename T>
struct with_cache {
using without_reference = typename std::remove_reference<T>::type;
using type = result<without_reference, ..., without_reference>;
};
template <typename T>
struct without_cache {
using type …Run Code Online (Sandbox Code Playgroud) c++ templates instantiation boost-mpl template-meta-programming
考虑一下这段代码,
template<bool b>
struct other
{
static const bool value = !b;
};
template<bool b>
struct test
{
static const bool value = b || other<b>::value;
};
int main()
{
bool value = test<true>::value;
}
Run Code Online (Sandbox Code Playgroud)
编译器是否other<true>在上述情况下实例化,实例化时似乎完全没必要?或者仅仅因为我编写了语法other<b>::value,编译器必须实例化它,而不管它是否对计算值的贡献完全没有贡献test<true>::value?
我想听听,a)标准需要什么,以及b)各种编译器实际实现了什么?标准的相关部分将不胜感激.
我正在尝试列出我在Matlab文件夹中的某个文件夹中创建的类 - 仅使用其名称(类名)
作为一个例子,我有一个名为'SimpleString'的类 - 我的目标是从该类实例化一个对象,如果我知道它的名字是'SimpleString'
所以实时,我想找出文件夹中的类(完成),然后能够实例化任何这些类(我的问题)谢谢
是否可以在verliog中有条件地实例化模块?
例如:
if (en==1)
then module1 instantiation
else
module2 instantiation
Run Code Online (Sandbox Code Playgroud) 如果想要创建泛型的新实例,则需要定义新约束,如下所示:
public T SomeMethod<T>() where T : new()
{
return new T();
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用反射在没有新约束的情况下创建T的实例,如此(包含伪代码):
public T SomeMethod<T>()
{
if (T has a default constructor)
{
return a new instance of T;
}
else
{
return Factory<T>.CreateNew();
}
}
Run Code Online (Sandbox Code Playgroud) 我(含糊地)知道如果不 使用模板,则不会实例化模板.例如,以下代码将编译正常,即使T::type没有意义T = int.
template<typename T>
struct A
{
void f() { using type = typename T::type; }
};
A<int> a; //ok
Run Code Online (Sandbox Code Playgroud)
它编译因为f()没有使用,所以它没有被实例化 - 因此T::type仍然没有检查的有效性.其他成员函数g()调用是否无关紧要f().
template<typename T>
struct A
{
void f() { using type = typename T::type; }
void g() { f(); } //Is f() still unused?
};
A<int> a; //ok
Run Code Online (Sandbox Code Playgroud)
这也是罚款.但在这里,我意识到我对"使用"定义的理解模糊.我问:
f()仍未使用?究竟怎么样?我可以清楚地看到它被用在里面g() …