标签: instantiation

对象创建期间的java覆盖

在下面的java代码中创建了一个JButton,但同时它的一个方法被覆盖.问题:创建对象时是否有以这种方式覆盖的名称?

代码:

   JButton myButton;
   myButton = new JButton ("ok"){

        @Override
        public void setText(String text) {
            super.setText(text +", delete");
        }
Run Code Online (Sandbox Code Playgroud)

jbutton的标签现在是"ok,delete"

java overriding instantiation

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

C++模板静态成员实例化

#include <map>
#include <iostream>
template <typename T>
class A 
{
 static std::map<int, int> data;
public:
 A()
 {
  std::cout << data.size() << std::endl;
  data[3] = 4;
 }
};

template <typename T>
std::map<int, int> A<T>::data;

//std::map<int, int> A<char>::data;

A<char> a;

int main()
{
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

这有什么问题?如果没有明确的实例化,它就会破坏

 data[3] = 4; 
显式实例化解决了问题但程序在之后中断
std::cout << data.size() << std::endl;
什么意思是静态类模板memeber data被实例化.

c++ static templates instantiation member

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

为什么编译器试图实例化一个我实际上没有在任何地方实例化的模板?

更新如下.
以下是我在main.cpp中的完整代码:

template<class T>
struct other_traits;

template<class T>
struct some_traits{
    typedef decltype(&T::operator()) Fty;
    typedef typename other_traits<Fty>::type type;
};

int main(){
}
Run Code Online (Sandbox Code Playgroud)

但是我在Visual Studio 2010中遇到以下错误,而g ++编译得很好:

src\main.cpp(9):错误C2146:语法错误:缺少';' 在标识符'type'之前
--src\main.cpp(10):参见类模板实例化' some_traits<T>'正在编译
src\main.cpp(9):错误C2868:' some_traits<T>::type':using声明的非法语法; 预期的合格名称

(我喜欢最后一个,总重量.)

我可以将其视为VC10中的错误,还是有充分的理由进行早期实例化?或者它是一个错误decltype,使编译器认为这Fty不是一个依赖名称?


更新:我试图欺骗编译器,认为这Fty是一个依赖名称,使用基类继承:

template<class T>
struct other_traits;

template<class R, class C>
struct other_traits<R (C::*)()>{
    typedef R type;
};

template<class Fty>
struct base_traits{
    typedef typename other_traits<Fty>::type type;
};

template<class T>
struct some_traits
    : public base_traits<decltype(&T::operator())>
{};
Run Code Online (Sandbox Code Playgroud)

但是编译器仍然试图在现场实例化/编译所有内容,引发这些错误: …

c++ templates instantiation visual-studio-2010 c++11

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

什么决定什么时候在PHP中销毁类对象?

让我们说我们上课了CFoo.在以下示例中CFoo::__destruct()调用时?

function MyPHPFunc()
{
  $foo = new CFoo();

  . . .

  // When/where/how does $foo get destroyed/deleted?
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,当脚本退出范围时会调用析构函数MyPHPFunc因为$foo不再可访问吗?

php destructor class instantiation

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

Android SDK错误:尝试实例化不是片段的类

我很难创建一个简单的应用程序,顶部菜单和下面的可更改视图(通过按菜单片段中的按钮我们更改下面片段的视图).所以,我在主视图中有2个片段,但是当我试图在模拟器中运行应用程序时,我得到一个错误:

Cause by android.app (bla bla bla, piece of crap Eclipse doesn't even allow copying the errors): 
Trying to instantiate a class com.example.android.topmenu that is not a fragment
Run Code Online (Sandbox Code Playgroud)

所以,这些是我的XML布局:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/menuFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:name="com.example.android.topmenu" >
    </fragment>

    <fragment
        android:id="@+id/contentFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.example.android.bottomcontent" >
    </fragment>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

topmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

   <Button
       android:id="@+id/Button1"
       android:layout_width="wrap_content"
       android:layout_height="match_parent" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

bottom_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" …
Run Code Online (Sandbox Code Playgroud)

sdk android class instantiation fragment

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

类模板实例化

我刚刚阅读了有关CRTP的wiki文章,我对模板实例化有点困惑.

根据维基,

成员函数体(定义)直到它们的声明后很久才被实例化.

我不太明白这意味着什么.

假设我有一个类模板:

template <typename T>
class A
{
    public:
        void foo(T t)
        {
            //...
        };
};
Run Code Online (Sandbox Code Playgroud)

当我实例化类模板A时,它是否实例化成员函数foo()?

例如:

//in .cpp file
int main()
{
    A<int> a; //question 1
              //class template is instantiated here, isn't it? 
              //What about foo(), is it instantiated too?

    a.foo(10); //question 2
               //according to the quotation, foo() will not be instantiated until it is used.
               //if so, foo() is instantiated right here, not in question 1, right?
}
Run Code Online (Sandbox Code Playgroud)

c++ templates instantiation

14
推荐指数
2
解决办法
9786
查看次数

是什么让java中的枚举不可实例化?

我知道这是一个枚举

enum Year
{
   First, Second, Third, Fourth;
}
Run Code Online (Sandbox Code Playgroud)

被转换成

final class Year extends Enum<Year>
{
        public static final Year First = new Year();
        public static final Year Second = new Year();
        public static final Year Third = new Year();
        public static final Year Fourth = new Year();
}
Run Code Online (Sandbox Code Playgroud)

当我尝试实例化枚举(不是类)时,我得到编译时错误:

error: enum types may not be instantiated
        Year y = new Year();
Run Code Online (Sandbox Code Playgroud)

据我所知,私有构造函数使类不可实例化.我认为编译器提供了一个私有构造函数.但是当我看到我们可以使用默认修饰符为枚举定义构造函数时仍然无法创建枚举类型的对象时,我感到很困惑.

enum Year
{
        First, Second, Third, Fourth;
        Year()
        {
        }
}

class Example
{
        public static void …
Run Code Online (Sandbox Code Playgroud)

java enums constructor instantiation

14
推荐指数
2
解决办法
3421
查看次数

使用__new__从现有对象中获取Python对象

在学习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)

python types object instantiation

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

是否可以在编译之前使用实例化模板查看C++代码(g ++)?

g ++编译器有一个生成宏扩展代码(-E)的标志,所以我想知道在实际编译发生之前是否有一种方法可以在模板实例化之后查看程序.

c++ templates g++ instantiation abstract-syntax-tree

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

通过缓存元函数优化编译时性能

假设我有以下元函数:

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)

我看到两种可能性:

  1. 编译器每次看到它都必须做一些工作typename std::remove_reference<T>::type.使用中间别名具有某种"缓存"行为,这允许编译器只执行一次工作.

  2. 编译时性能是根据编译器必须执行的模板实例化的数量来衡量的.因为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

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