小编eXX*_*XX2的帖子

Collection.toArray()java.lang.ClassCastException

import java.util.HashMap;
import java.util.Map;


public class Main
{
    public static void main(String[] args)
    {
        Map<Integer,Class> map=new HashMap<Integer,Class>();
        map.put(0,Main.class);
        Class[] classes=(Class[])map.values().toArray();
        for (Class c:classes)
            System.out.println(c.getName());
    }

}
Run Code Online (Sandbox Code Playgroud)

我试着在这一行投,Class[] classes=(Class[])map.values().toArray();但得到例外.

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Class; at Main.main(Main.java:11)

有什么问题?

java collections

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

共享库中静态对象的销毁顺序

我有一个主程序(main.cpp)和一个共享库(test.htest.cpp):

test.h:

#include <stdio.h>

struct A {
    A() { printf("A ctor\n"); }
    ~A() { printf("A dtor\n"); }
};

A& getA();
Run Code Online (Sandbox Code Playgroud)

TEST.CPP:

#include "test.h"

A& getA() {
    static A a;
    return a;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

#include "test.h"

struct B {
    B() { printf("B ctor\n"); }
    ~B() { printf("B dtor\n"); }
};

B& getB() {
    static B b;
    return b;
}

int main() {
    B& b = getB();
    A& a = getA();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我在Linux上编译这些源代码的方法:

g++ …
Run Code Online (Sandbox Code Playgroud)

c++ windows singleton shared-libraries object-destruction

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

setSize()不适用于JFrame

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

class Demo
{
    JFrame jf;
    Demo()
    {
        jf=new JFrame("Demo");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(5000,5000);
        jf.setVisible(true);
        System.out.println(jf.getSize());
    }
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                new Demo();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

jf.setSize(5000, 5000)用于JFrame,但之后getSize返回其他大小:( java.awt.Dimension[width=1386,height=788]我的屏幕分辨率是1366x768)我可以设置大于屏幕大小的帧大小吗?可能这样的行为提供了一些框架属性,但我不知道它们.

java swing jframe

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

rdtsc,周期太多了

#include <stdio.h>
static inline unsigned long long tick() 
{
        unsigned long long d;
        __asm__ __volatile__ ("rdtsc" : "=A" (d) );
        return d;
}

int main()
{
        long long res;
        res=tick();

        res=tick()-res;
        printf("%d",res);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

我用gcc编译了这段代码,并使用了-O0 -O1 -O2 -O3优化.我总是得到2000-2500个周期.任何人都可以解释这个输出的原因吗?如何度过这些周期?

第一个函数"tick"是错误的.这是对的.

另一个版本的功能"滴答"

static __inline__ unsigned long long tick()
{
  unsigned hi, lo;
  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
Run Code Online (Sandbox Code Playgroud)

这是-O3的汇编代码

 .file  "rdtsc.c"
.section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
    .string "%d"
    .text
    .p2align 4,,15
.globl …
Run Code Online (Sandbox Code Playgroud)

c x86 assembly rdtsc

8
推荐指数
3
解决办法
7630
查看次数

在其他构造函数中调用复制构造函数

#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
class A
{
public:
    std::string s;
    A()
    {
        s = "string";
        new(this)A(*this);
    }
};
int main()
{
    A a;
    std::cout<<a.s;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在输出中得到空字符串.C++标准对这种行为有何看法?

c++ placement-new

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

JLabel画点

import java.awt.Graphics;
import javax.swing.*;

public class Demo
{
    JFrame jf;
    JLabel[] labels;
    JPanel panel;

    public Demo()
    {
        jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        labels = new JLabel[10];
        Box vbox = Box.createVerticalBox();
        for (int i = 0; i < 10; i++)
        {
            labels[i] = new JLabel();
            vbox.add(labels[i]);
        }
        panel = new JPanel();
        panel.add(vbox);
        jf.add(panel);
        jf.setSize(300, 250);
        jf.setVisible(true);
    }
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new DemoRunnable());
    }
    public void updateState()
    {
        for (JLabel l : labels)
        {
            if (Math.random() > 0.5)
                l.setText("777777777777777777777777777777777777"); …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel

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

使用虚拟继承和委托构造函数在构造函数中崩溃

struct D
{
    virtual void m() const = 0;
};

struct D1 : public virtual D { };

struct D2 : public virtual D { };

struct B : public D2
{
    B() { }

    B(int val) : B() { }

    void m() const { }
};

struct A : public B, public D1
{
    A() : B(0) { }
};

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

使用上面的代码我遇到MSVC 2013编译器崩溃.使用GCC 4.7.2编译时,它运行时没有崩溃.类的层次结构如下所示.

         D
       /  \
     D1    D2
      |     |
       \ …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-inheritance visual-c++ c++11 delegating-constructor

8
推荐指数
2
解决办法
510
查看次数

mycout自动endl

我想实现类MyCout,它可以提供自动endl的可能性,即这个代码

MyCout mycout;
mycout<<1<<2<<3;
Run Code Online (Sandbox Code Playgroud)

输出

123
//empty line here
Run Code Online (Sandbox Code Playgroud)

是否可以实现具有此类功能的类?


更新:Soulutions不应该是那样的,MyCout()<<1<<2<<3;即它们应该没有创建临时对象

c++ cout

7
推荐指数
3
解决办法
989
查看次数

海湾合作委员会似乎错过了简单的优化

我试图用三元运算符的语义引入泛型函数:E1 ? E2 : E3.我看到编译器能够消除三元运算符之一E2E3取决于E1条件的计算.然而,GCC在ternary函数调用的情况下错过了这种优化(即使E2/ E3没有副作用).

在下面的清单中ternary,写入函数的行为类似于三元运算符.然而,GCC发出可能对函数的大量调用,f这似乎可以消除某些输入值(确切地说它是如何为三元运算符完成的)因为f使用纯属性声明 - 请查看GCC生成的汇编代码的godbolt链接.

它是否可以在GCC(优化空间)中得到改进,或者C++标准是否明确禁止这种优化?

// Very heavy function
int f() __attribute__ ((pure));

inline int ternary(bool cond, int n1, int n2) {
    return cond ? n1 : n2;
}

int foo1(int i) {
    return i == 0 ? f() : 0;
}

int foo2(int i) {
    return ternary(i == 0, f(), 0);
}
Run Code Online (Sandbox Code Playgroud)

装配清单-O3 -std=c++11 …

c++ optimization assembly gcc

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

模板类中的类型转换运算符 - 无论显式如何都被调用

我有这个代码.在主要我想要使用类型转换,但使用调试我明白在这一行 ob2=(Point2D<double>)ob1;

template <class T1> Point2D(Point2D<T1>& ob)无论explicit以前调用构造函数,template <class T1> explicit Point2D(Point2D<T1>& ob)为什么会发生这种情况?我希望这operator Point2D<T1>()是被调用的.

template <class T>
class Point2D
{
public:
    T x;
    T y;
    Point2D(T _x=0,T _y=0):x(_x),y(_y)
    {
    }
    Point2D(Point2D& ob)
    {
        x=ob.x;
        y=ob.y;
    }
    template <class T1>
    Point2D(Point2D<T1>& ob)
    {
        x=ob.x;
        y=ob.y;
    }
    template <class T1>
    operator Point2D<T1>()
    {
        return Point2D<T1>(x,y);
    }
};
int main()
{
    Point2D<int> ob1(10,10);
    Point2D<double> ob2(20,20);
    ob2=(Point2D<double>)ob1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates type-conversion

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